欧拉函数 原根 POJ 1284 Primitive Roots
Primitive Roots
Description
We say that integer x, 0 < x < p, is a primitive root modulo odd prime p if and only if the set { (xi 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
题目大意就是给出一个奇素数,求出他的原根的个数,定义n的原根x满足条件0<x<n,并且有集合{ (xi mod n) | 1 <= i <=n-1 } 和集合{ 1, ..., n-1 }相等
关于这道题。如果知道欧拉函数的话,看出的答案是phi(n-1)其实也不难
定理1:如果p有原根,则它恰有φ(φ(p))个不同的原根(无论p是否为素数都适用) p为素数,当然φ(p)=p-1,因此就有φ(p-1)个原根
AC代码:
View Code
1 #include<stdio.h> 2 int main() 3 { 4 int p,rea,i; 5 while(scanf("%d",&p)!=EOF) 6 { 7 p--; 8 rea=p; 9 for(i=2;i*i<=p;i++) 10 { 11 if(p%i==0) 12 { 13 rea=rea-rea/i; 14 do{ 15 p/=i; 16 }while(p%i==0); 17 } 18 } 19 if(p>1) 20 rea=rea-rea/p; 21 printf("%d\n",rea); 22 } 23 return 0; 24 }
posted on 2012-08-13 14:20 jumpingfrog0 阅读(310) 评论(0) 编辑 收藏 举报