POJ1284:Primitive Roots——题解

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

Given an odd prime p, find the number of primitive roots of p.

There is a conclusion: when a positive integer n has a primitive root, it has a total of phi(phi(n)) primitive roots with different remainders.

So the answer is 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;
}

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

+ Author of this article: luyouqi233. +

+Welcome to my blog: http://www.cnblogs.com/luyouqi233/  +

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

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

+ Author of this article: luyouqi233. +

+Welcome to my blog: http://www.cnblogs.com/luyouqi233/  +

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325255494&siteId=291194637