Primitive Roots
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 3928 | Accepted: 2342 |
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
1 对于给出的素数p, 2 首先要明确一点:p的元根必然是存在的(这一点已由Euler证明,此处不再赘述),因此,不妨设其中的一个元根是a0(1<=a0<=p-1) 3 按照题目的定义,a0^i(1<=i<=p-1) mod p的值是各不相同的,再由p是素数,联系Fermat小定理可知:q^(p-1) mod p=1;(1<=q<=p-1)(这个在下面有用) 4 下面证明,如果b是p的一个异于a的元根,不妨令b与a0^t关于p同余,那么必然有gcd(t,p-1)=1,亦即t与p-1互质;反之亦然; 5 证明: 6 若d=gcd(t,p-1)>1,令t=k1*d,p-1=k2*d,则由Fermat可知 7 (a0^(k1*d))^k2 mod p=(a0^(k2*d))^(k1) mod p=(a0^(p-1))^(k1) mod p=1 8 再由b=a0^t (mod p),结合上面的式子可知: 9 (a0^(k1*d))^k2 mod n=b^k2 mod p=1; 10 然而b^0 mod p=1,所以b^0=b^k2 (mod p),所以b^i mod p的循环节=k2<p-1,因此这样的b不是元根; 11 12 再证,若d=gcd(t,p-1)=1,即t与p-1互质,那么b必然是元根; 13 否则假设存在1<=j<i<=p-1,使得b^j=b^i (mod p),即a0^(j*t)=a0^(i*t) (mod p),由a0是元根,即a0的循环节长度是(p-1)可知,(p-1) | (i*t-j*t)->(p-1) | t*(i-j),由于p与 14 t互质,所以(p-1) | (i-j),但是根据假设,0<i-j<p-1,得出矛盾,结论得证; 15 16 由上面的两个证明可知b=a0^t (mod p),是一个元根的充要条件是t与p-1互质,所有的这些t的总个数就是Phi(p-1);