poj_1284_Primitive root
We say that integer x, 0 < x < p, is a primitive root modulo odd prime p if and only if the set { (x i mod p) | 1 <= i <= p-1 } is equal to { 1, ..., p-1 }. For example, the consecutive powers of 3 modulo 7 are 3, 2, 6, 4, 5, 1, and thus 3 is a primitive root modulo 7.
Write a program which given any odd prime 3 <= p < 65536 outputs the number of primitive roots modulo p.
Write a program which given any odd prime 3 <= p < 65536 outputs the number of primitive roots modulo p.
Input
Each line of the input contains an odd prime numbers p. Input is terminated by the end-of-file seperator.
Output
For each p, print a single number that gives the number of primitive roots in a single line.
Sample Input
23 31 79
Sample Output
10 8 24
求模素数原根的方法:对
素因子分解,即
是
的标准分解式,若恒有
成立,则就是
的原根。(对于合数求原根,只需把
换成
即可)
定义:设,
,使得
成立的最小的
,称为
对模
的阶,记为
。
定理:如果模有原根,那么它一共有
个原根。
定理:若,
,
,则
。
定理:如果为素数,那么素数
一定存在原根,并且模
的原根的个数为
。
定理:设是正整数,
是整数,若
模
的阶等于
,则称
为模
的一个原根。
假设一个数对于模
来说是原根,那么
的结果两两不同,且有
,那么
可以称为是模
的一个原根,归根到底就是
当且仅当指数为
的时候成立。(这里
是素数)
#include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #define maxn 65550 using namespace std; typedef long long ll; int phi[maxn]; int a[maxn]; bool vis[maxn]; int val[maxn]; int prime[maxn],pn=0; int main() { int i,j; for(i=1; i<=maxn; i++) phi[i]=i; for(i=2; i<=maxn; i+=2) phi[i]/=2; for(i=3; i<=maxn; i+=2) if(phi[i]==i) { for(j=i; j<=maxn; j+=i) phi[j]=phi[j]/i*(i-1); } int n; while(~scanf("%d",&n)) { cout<<phi[n-1]<<endl; } }