【POJ1284】Primitive Roots——欧拉数

Primitive Roots

Time Limit: 1000MS Memory Limit: 10000K

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

题意:求奇素数的原根的个数。

分析:
(a,m)=1dm1d=φ(m)mm使ad1(modm)(a,m)=1amδm(a)使ad1(modm)dδm(a)φ(m)δm(a)=φ(m)am
φ(φ(m))
ppφ(p1)
所以对于这道题来说就是求欧拉数。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <iostream>

using namespace std;

const int Max = 66536;

int Phi[Max];

void Init()//处理欧拉数
{
    memset(Phi,0,sizeof(Phi));

    Phi[1] = 1;

    for(int i = 2;i<Max;i++)
    {
        if(!Phi[i])
        {
            for(int j = i;j<Max;j+=i)
            {
                if(!Phi[j]) Phi[j]  =j;

                Phi[j] = Phi[j]/i*(i-1);
            }
        }
    }
}
int main()
{
    int n;

    Init();

    while(~scanf("%d",&n))
    {
        printf("%d\n",Phi[n-1]);
    }
    return 0;
}

你可能感兴趣的:(【POJ1284】Primitive Roots——欧拉数)