Primitive Roots
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6187 Accepted: 3529
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.
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
题目大概意思:
给出一个素数 ,求模 的原根的个数。
分析:
根据以下两个定理即可解决此题:
- 模 有原根的充要条件是 ,其中 是奇素数, 是任意正整数。
- 当模 有原根时,它有 个原根。
(其中 为欧拉函数)
由于
为大于
的素数,因此一定有原根。我们预处理出
的欧拉函数值的表,再根据定理计算答案即可。
下面贴代码:
#include <cstdio>
using namespace std;
const int MAX_N = 65536;
int euler[MAX_N];
void euler_phi();
int main()
{
int p;
euler_phi();
while (~scanf("%d", &p))
{
printf("%d\n", euler[euler[p]]);
}
return 0;
}
void euler_phi()
{
for (int i = 1; i < MAX_N; ++i)
{
euler[i] = i;
}
for (int i = 2; i < MAX_N; ++i)
{
if (euler[i] == i)
{
for (int j = i; j < MAX_N; j += i)
{
euler[j] = euler[j] / i * (i - 1);
}
}
}
}