1. 程式人生 > >POJ1284:Primitive Roots——題解

POJ1284:Primitive Roots——題解

OS http 不同 span include else stream www. TP

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/ +

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

POJ1284:Primitive Roots——題解