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;
}
博客介绍了质数p的原始根的概念,即一个整数x(1≤x<p),使得x的幂次从x^2-1到x^p-1-1都不被p整除,但x^p-1-1可以。问题要求帮助计算给定质数p的原始根的数量。输入为一个质数p(2≤p<2000),输出是原始根的个数。示例中,当p=2时,唯一的原始根是2;当p=3时,原始根为2和3。
1711

被折叠的 条评论
为什么被折叠?



