Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 3381 | Accepted: 1980 |
Description
Write a program which given any odd prime 3 <= p < 65536 outputs the number of primitive roots modulo p.
Input
Output
Sample Input
23 31 79
Sample Output
10 8 24
一個數是數n的原根就必然與n-1互質,所以求n的原根的數量即是求歐拉函數n-1。
代碼:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;
long long euler(long long n)
{
long long res = n, a = n;
for (long long i = 2; i*i <= a; i++)
{
if (a%i == 0)
{
res = res / i*(i - 1);
while (a%i == 0)a /= i;
}
}
if (a > 1)res = res / a*(a - 1);
return res;
}
int main()
{
long long n;
while (cin >> n)
{
cout << euler(n-1) << endl;
}
return 0;
}