pku1284
Primitive Roots
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 3882 | Accepted: 2301 |
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.
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
题目大意:求1-n中与n互素的数的个数
思路:利用原根的性质 对正整数gcd(a,m) = 1,如果 a 是模 m 的原根,
那么 a 是整数模n乘法群(即加法群 Z/mZ的可逆元,也就是所有与 m
素的正整数构成的等价类构成的乘法群)Zn的一个生成元。由于Zn有
φ(m)个元素,而它的生成元的个数就是它的可逆元个数,
即 φ(φ(m))个,因此当模m有原根时,它有φ(φ(m))个原根,
又因为该题的m是素数,所以 φ(m)=m-1.
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
bool book[50000];
int p[20000];
void prim()
{
memset(book,false,sizeof(book));
book[0]=book[1]=1;
int k=0;
for(int i=2;i<50000;i++)
{
if(!book[i])p[k++]=i;
for(int j=0;j<k&&i*p[j]<50000;j++)
{
book[i*p[j]]=1;
if(!(i%p[j]))break;
}
}
}
LL getphi(LL n)
{
LL rea=n;
for(int i=0;p[i]*p[i]<=n;i++)if(n%p[i]==0)
{
rea-=rea/p[i];
while(n%p[i]==0)n/=p[i];
}
if(n>1)rea=rea-rea/n;
return rea;
}
int main()
{
LL n;
prim();
while(scanf("%lld",&n)==1&&n)cout<<getphi(n-1)<<endl;
}