A. Cows and Primitive Roots
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The cows have just learned what a primitive root is! Given a prime p, a primitive root is an integer x (1 ≤ x < p) such that none of integers x - 1, x2 - 1, ..., xp - 2 - 1 are divisible by p, but xp - 1 - 1 is.
Unfortunately, computing primitive roots can be time consuming, so the cows need your help. Given a prime p, help the cows find the number of primitive roots .
Input
The input contains a single line containing an integer p (2 ≤ p < 2000). It is guaranteed that p is a prime.
Output
Output on a single line the number of primitive roots .
Sample test(s)
input
3
output
1
input
5
output
2
Note
The only primitive root is 2.
The primitive roots are 2 and 3.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;
int main(){
int p;
cin>>p;
int ans=0;
for(int x = 1; x < p; x++) //遍历x
{
int tmp=1;
for(int j=1; j<p; j++) //x的j次方
{
tmp*=x;
tmp%=p; //防止数据溢出,求余操作分歩
if(tmp==1)//即满足(x^tmp - 1)% p == 0
{
if(j==p-1) // 即符合题意 (x^(p-1) - 1)% p == 0
ans++;
break;
}
}
}
cout<<ans<<endl;
return 0;
}