问题描叙:
We say that integer x, 0 < x < p, is a primitive root modulo odd prime p if and only if the set { (x i 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 79Sample Output
10 8 24
题目题意:题目给我们一个奇素数,让我们求有多少个x满足the set { (x i mod p) | 1 <= i <= p-1 } is equal to { 1, ..., p-1 }.
题目分析:其实这个题目实质就是问p有多少个原根(哎,想要学好数论,得见多识广啊!)
下面是百度对原根的解释:
对于这个题目来说有俩点重要的原根性质
1:模m有原根的充要条件是m= 1,2,4,p,2p,p^n,其中p是奇质数,n是任意正整数。
2:对正整数(a,m) = 1,如果 a 是模 m 的原根,那么 a 是整数模n乘法群(即加法群 Z/mZ的可逆元,也就是所有与 m 互素的正整数构成的等价类构成的乘法群)Zn的一个生成元。由于Zn有 φ(m)个元素,而它的生成元的个数就是它的可逆元个数,即 φ(φ(m))个,因此当模m有原根时,它有φ(φ(m))个原根。
个人实力太差了,不能给大家解释了(还得赶紧多学点才行)
我们就记住吧!模m有原根,m=1,2,4,p,2p,p^n,p是奇质数,当模m有原根时,原根的个数是φ(φ(m))(这个是欧拉函数)
因为这个题目给的n是奇素数,所以phi[n]=n-1,因为n与1到n-1都互质。
答案就是phi[n-1]
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int get_euler(int n)
{
int res=n,a=n;
for (int i=2;i*i<=a;i++) {
if (a%i==0) {
res=res/i*(i-1);
while (a%i==0) a=a/i;
}
}
if (a>1) res=res/a*(a-1);
return res;
}
int main()
{
int n;
while (scanf("%d",&n)!=EOF) {
printf("%d\n",get_euler(n-1));
}
return 0;
}