POJ_1284 Primitive Roots 【原根性质+欧拉函数运用】

一、题目

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
79

Sample Output

10
8
24

二、题意分析

原根并不像百度介绍的那样,需要深入去研究。直接上干货,《初等数论及应用》(第六版)P260 定理9.5

如果正整数n有一个原根,那么它一共有φ(φ(n))个不同的原根。

对应该题目,因为给的p是奇素数,所以答案就是φ(p-1)

三、代码

#include <iostream>
#include <cstring>
using namespace std;

const int MAXN = 66000;
int Prime[MAXN], nPrime;
bool isPrime[MAXN];

void make_prime()
{
    memset(isPrime, 1, sizeof(isPrime));
    nPrime = 0;
    isPrime[0] = isPrime[1] = 0;
    for(int i = 2; i < MAXN; i++)
    {
        if(isPrime[i])
            Prime[nPrime++] = i;
        for(int j = 0; j < nPrime && (long long)i*Prime[j] < MAXN; j++)
        {
            isPrime[i*Prime[j]] = 0;
            if(i%Prime[j] == 0)
                break;
        }
    }
}

int Euler(int p)
{
    int ans = p;
    for(int i = 0; Prime[i]*Prime[i] <= p ; i++)
    {
        if( p % Prime[i] == 0)
        {
            ans = ans - ans/Prime[i];
            do
            {
                p /= Prime[i];
            }while(p%Prime[i] == 0);
        }
    }
    if(p > 1)
        ans = ans - ans/p;
    return ans;
}

int main()
{
    int p;
    make_prime();
    while( cin >> p )
    {
        cout << Euler(p-1) << endl;
    }
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/dybala21/p/9747570.html