POJ1284_Primitive Roots【欧拉函数】

Primitive Roots

Time Limit: 1000MS
Memory Limit: 10000K

Total Submissions: 3032Accepted: 1734

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

贾怡@pku

题目大意:p是奇素数,如果{x^i % p | 1 <= i <= p - 1} = {1,2,...,p-1},则称x是p的原根。

给出一个p,问它的原根有多少个。

思路:

{x^i% p | 1 <= i <= p - 1} = {1,2,...,p-1} 等价于

{x^i%(p-1) | 1 <= i <= p - 1} = {0,1,2,...,p-2},

即{x^1,x^2,x^3,…,x^(p-1)}为p的完全剩余系等价于

若x与p-1互质(gcd(x, p-1) = 1),则{x^0,x^1,x^2,…,x^(p-2)}为(p-1)的完全剩余系

下边证明:

如果x^i != x^j (mod p-1),那么 x*x^i != x*x^j (mod p-1),则gcd(x, p-1) != 0,与上边相悖。

则x^i = x^j(mod p-1)。

根据费马定理和欧拉定理知:i = φ(p-1)。

关于欧拉函数、费马定理和欧拉定理参考另一篇欧拉函数的题解:

http://blog.csdn.net/lianai911/article/details/40114675

参考博文:http://www.cnblogs.com/lnever/p/3969117.html

顺便膜拜推理出来的大神

#include <stdio.h>
#include <math.h>

int Euler(int n)
{
    int i, ret = n;
    for(i = 2; i <= sqrt(1.0*n); i++)
    {
        if(n % i == 0)
        {
            n /= i;
            ret = ret - ret/i;
        }
        while(n % i == 0)
            n /= i;
    }
    if(n > 1)
    {
        ret = ret - ret/n;
    }
    return ret;
}

int main()
{
    int p;
    while(~scanf("%d",&p))
    {
        printf("%d\n",Euler(p-1));
        //如果p为素数,Euler(Euler(p)) == Euler(p-1)
    }
    return 0;
}
时间: 2024-12-25 20:01:12

POJ1284_Primitive Roots【欧拉函数】的相关文章

POJ 1284-Primitive Roots(欧拉函数求原根)

Primitive Roots Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1284 Appoint description:  System Crawler  (2015-04-06) Description We say that integer x, 0 < x < p, is a primitive root mod

【poj1284】Primitive Roots 欧拉函数

题目描述: 题意: 定义原根:对于一个整数x,0<x<p,是一个mod p下的原根,当且仅当集合{ (xi mod p) | 1 <= i <= p-1 } 等于{ 1, ..., p-1 }.给定p,询问有多少个满足定义的原根. 这里有一个定理:如果p有原根,则它恰有φ(φ(p))个不同的原根 证明不懂就算了,我也不懂啊TAT 证明如下 题目中说m是奇素数,所以φ(p)=p-1,故ans=φ(p-1). 代码如下: 1 #include<cstdio> 2 #incl

POJ 1284 Primitive Roots 欧拉函数模板题

#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <cmath> #include <stack> #include <map> #include <ctime> #include <io

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 t

欧拉函数

void Euler_Sieve_Method(int * euler, int n) { euler[1] = 1; for (int i = 2; i < n; i++) { euler[i] = i; } for (int i = 2; i < n; i++) { if (euler[i] == i) { for (int j = i; j < n; j += i) { euler[j] = euler[j] / i * (i - 1); } } } } void Euler_Si

hdu1695(莫比乌斯)或欧拉函数+容斥

题意:求1-b和1-d之内各选一个数组成数对,问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个可以简化成1-b/k 和1-d/k 的互质有序数对的个数.假设b=b/k,d=d/k,b<=d.欧拉函数可以算出1-b与1-b之内的互质对数,然后在b+1到d的数i,求每个i在1-b之间有多少互质的数.解法是容斥,getans函数参数的意义:1-tool中含有rem位置之后的i的质因子的数的个数. 在 for(int j=rem;j<=factor[i

欧拉函数常用性质

欧拉函数定义:设n 为正整数,则1,2......,n中与n互质的整数个数记作f(n). 1.1 若n为素数,f(n)=n-1; 1.2 整数n=p*q,p,q为不同素数,则f(n)=f(p)*f(q)=(p-1)*(q-1) 1.3 n=p^a*q^b,f(n)=f(p^a)*f(q^b)=n*(1-1/p)*(1-1/q) 1.4 分解质因子相乘,f(n)=n*(1-1/p1)*(1-1/p2)*.......*(1-1/pk). f(100)=f(2^2*5^2)=100*1/2*4/5=

POJ2478(SummerTrainingDay04-E 欧拉函数)

Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16927   Accepted: 6764 Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b)

POJ 2478 欧拉函数(欧拉筛法) HDU 1576 逆元求法

相关逆元求法,我之前有写过,还有欧拉函数的求法,欧拉函数与逆元的关系  点击 POJ 2478 又是一个打表的题目,一眼看出结果就是前n个欧拉函数值的和. 这里直接计算欧拉函数值求和会超时,看见多组数据. 然后就是计算欧拉函数,打表就好了. #include <stdio.h> #include <string.h> #include <iostream> using namespace std; typedef long long LL; const int N =