Primitive Roots

Time Limit: 1000MSMemory Limit: 10000K

Total Submissions: 5709Accepted: 3261

Description

We say that integer x, 0 < x < p, is a primitive root modulo odd prime p if and only if the set (ximodp)∣1≤i≤p−1{ (x_i mod p) | 1 \leq i \leq p-1 }(xi​modp)∣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.

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

题意

给出一个正素数p,求p的原根的个数

思路

原根的定义: 对于两个正整数(a,m)=1(a,m)=1(a,m)=1,由欧拉定理可知:存在d≤m−1d\leq m-1d≤m−1。比如说欧拉函数d=φ(m)d=φ(m)d=φ(m),即小于等于mmm的正整数与mmm互质的正整数的个数,使得ad≡1(modm)a^d\equiv1 (mod m)ad≡1(modm)。由此,在(a,m)=1(a,m)=1(a,m)=1时,定义aaa对模mmm的指数δm(a)\delta m(a)δm(a)为使ad≡1(modm)a^d\equiv1(mod m)ad≡1(modm)成立的最小正整数ddd。由前知δm(a)\delta m(a)δm(a)一定小于等于φ(m)φ(m)φ(m),若δm(a)=φ(m)\delta m(a)=φ(m)δm(a)=φ(m),则称aaa为mmm的原根

原根个数定理: 如果ppp有原根,则它恰有φ(φ(p))φ(φ(p))φ(φ(p))个不同的原根,ppp为素数时,φ(p)=p−1φ(p)=p-1φ(p)=p−1,因此就有φ(p−1)φ(p-1)φ(p−1)个原根

AC代码

#include <iostream>
using namespace std;
int Eular(int n)
{
int eu=n;
for (int i=2;i*i<=n;i++)
{
if(n%i==0)
{
eu-=eu/i;
while(n%i==0)
n/=i;
}
}
if(n>1) //n本身也是个质因子
eu-=eu/n;
return eu;
}
int main(int argc, char const *argv[])
{
int p;
while(cin>>p)
{
cout<<Eular(p-1)<<endl;
}
return 0;
}
04-02 03:13