POJ1284---Primitive Roots(求原根个数, 欧拉函数)

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

Source

求模素数p的原根个数
关于原根请看这里:
原根

/************************************************************************* > File Name: POJ1284.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015年06月04日 星期四 16时04分32秒 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;


int phi[100000];
int minDiv[100000];

void geteuler() {
    for (int i = 1; i <= 70000; ++i) {
        minDiv[i] = i;
    }
    for (int i = 2; i <= 70000; ++i) {
        if (minDiv[i] == i) {
            if (70000 / i < i) {
                break;
            }
            for (int j = i * i; j <= 70000; j += i) {
                minDiv[j] = i;
            }
        }
    }
    phi[1] = 1; 
    for (int i = 2; i <= 70000; ++i) {
        phi[i] = phi[i / minDiv[i]];
        if ((i / minDiv[i]) % minDiv[i]) {
            phi[i] *= (minDiv[i] - 1);
        }
        else {
            phi[i] *= minDiv[i];
        }
    }
}

int main() {
    geteuler();
    int n;
    while (cin >> n) {
        cout << phi[n - 1] << endl;
    }
    return 0;
}

你可能感兴趣的:(欧拉函数)