POJ 1284 Primitive Roots 原根

时间:2022-12-05 08:55:30

题目来源:POJ 1284 Primitive Roots

题意:求奇素数的原根数

思路:一个数n是奇素数才有原根 原根数是n-1的欧拉函数

#include <cstdio>
const int maxn = 70000;
int phi[maxn];
void phi_table(int n)
{
for(int i = 2; i <= n; i++)
phi[i] = 0;
phi[1] = 1;
for(int i = 2; i <= n; i++)
if(!phi[i])
for(int j = i; j <= n; j += i)
{
if(!phi[j])
phi[j] = j;
phi[j] = phi[j] / i * (i-1);
}
} int main()
{
phi_table(65536);
int n;
while(scanf("%d", &n) != EOF)
{
printf("%d\n", phi[n-1]);
}
return 0;
}