关闭

poj 1284 Primitive Roots(求原根的个数)

标签: 原根欧拉函数
747人阅读 评论(0) 收藏 举报
分类:
Primitive Roots
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2590   Accepted: 1459

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
 
题意:对于一个数x(0<x<p),若集合{ (xi mod p) | 1 <= i <= p-1 }等于{ 1, ..., p-1 }.则称x为p的一个原根。给出一个数p,求p的原根的个数。
思路:利用了定理:如果n有原根,那么n的原根的数目就是euler(erler(n));由于题中说n为奇素数,故euler(n)就是(n-1),故答案即为euler(n-1);
 
 
AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>

#define ll __int64

using namespace std;
const int maxn = 70000;
int phi[maxn];
int n;
void phi_table()
{
    memset(phi, 0, sizeof(phi));
    phi[1] = 1;
    for(int i = 2; i < maxn; i++)
    if(!phi[i])
    {
        for(int j = i; j < maxn; j += i)
        {
            if(!phi[j]) phi[j] = j;
            phi[j] = phi[j] / i * (i - 1);
        }
    }
}
int main()
{
    phi_table();
    while(~scanf("%d", &n))
    {
        printf("%d\n", phi[n - 1]);
    }
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:244620次
    • 积分:6325
    • 等级:
    • 排名:第4090名
    • 原创:397篇
    • 转载:11篇
    • 译文:0篇
    • 评论:11条
    最新评论