POJ 1284 - Primitive Roots(欧拉函数)
题目链接 https://vjudge.net/problem/POJ-1284
【题意】
给定一个素数p,求p的原根个数。满足{ (x^i mod p) | 1 <= i <= p-1 } == { 1, …, p-1 }的x称为模p的原根。
相关定理如下
如果p有原根,则它恰有φ(φ(p))个不同的原根,φ为欧拉函数,p为素数,当然φ(p)=p-1,因此就有φ(p-1)个原根
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
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 (0 == phi[i]) {
for (int j = i; j <= n; j += i) {
if (0 == phi[j]) phi[j] = j;
phi[j] = phi[j] / i*(i - 1);
}
}
}
}
int main() {
phi_table(65536);
int n;
while (scanf("%d", &n) == 1) {
printf("%d\n", phi[n - 1]);
}
return 0;
}