POJ1284:Primitive Roots——题解

 http://poj.org/problem?id=1284

给一个奇质数p,求p的原根数量。

有一个结论:当正整数n存在原根时,其一共有phi(phi(n))个不同余的原根。

所以答案为phi(p-1)。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cctype>
#include<cstdio>
#include<cmath>
#include<stack>
using namespace std;
typedef long long ll;
const int N=70010;
int phi[N],su[N];
bool he[N];
void Euler(int n){
    phi[1]=1;
    int tot=0;
    for(int i=2;i<=n;i++){
        if(!he[i]){
            su[++tot]=i;
            phi[i]=i-1;
        }
        for(int j=1;j<=tot;j++){
            int p=su[j];
            if(i*p>n)break;
            he[i*p]=1;
            if(i%p==0){
                phi[i*p]=phi[i]*p;
                break;
            }else phi[i*p]=phi[i]*phi[p];
        }
    }
}
int main(){
    int n;
    Euler(N-10);
    while(scanf("%d",&n)!=EOF){
        printf("%d\n",phi[n-1]);
    }
    return 0;
}

+++++++++++++++++++++++++++++++++++++++++++

+本文作者:luyouqi233。               +

+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/ +

+++++++++++++++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++++++++++++++++++

+本文作者:luyouqi233。               +

+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/ +

+++++++++++++++++++++++++++++++++++++++++++

猜你喜欢

转载自www.cnblogs.com/luyouqi233/p/8990764.html