POJ 1284:Primitive Roots(素數原根的個數)
Primitive Roots
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5709 Accepted: 3261
Description
We say that integer x, 0 < x < p, is a primitive root modulo odd prime p if and only if the set 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
題意
給出一個正素數p,求p的原根的個數
思路
原根的定義: 對於兩個正整數,由尤拉定理可知:存在。比如說尤拉函式,即小於等於的正整數與互質的正整數的個數,使得。由此,在時,定義對模的指數為使成立的最小正整數。由前知一定小於等於,若,則稱為的原根
原根個數定理: 如果有原根,則它恰有個不同的原根,為素數時,,因此就有個原根
AC程式碼
#include <iostream>
using namespace std;
int Eular(int n)
{
int eu=n;
for (int i=2;i*i<=n;i++)
{
if(n%i==0)
{
eu-=eu/i;
while(n%i==0)
n/=i;
}
}
if(n>1) //n本身也是個質因子
eu-=eu/n;
return eu;
}
int main(int argc, char const *argv[])
{
int p;
while(cin>>p)
{
cout<<Eular(p-1)<<endl;
}
return 0;
}