poj 1284 Primitive Roots(未完)

Primitive Roots

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3155   Accepted: 1817

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

 1 #include<iostream>
 2 using namespace std;
 3 int euler(int a){
 4     int i=2;
 5     int ans=a;
 6     if(a%2==0){
 7         //cout<<2<<endl;
 8         ans=ans/i*(i-1);
 9         while(a%2==0){
10               a/=2;
11         }
12     }
13     for(i=3;i<=a;i+=2){//优化
14     if(a%i==0){
15         //cout<<i<<endl;
16         ans=ans/i*(i-1);
17         while(a%i==0){
18               a/=i;
19             }
20         }
21     }
22     return ans;
23 }
24 int main()//23, 28, and 33
25 {
26     int p;
27     while(cin>>p){
28         cout<<euler(p-1)<<endl;
29     }
30     return 0;
31 }
时间: 2024-12-28 15:57:27

poj 1284 Primitive Roots(未完)的相关文章

POJ 1284 Primitive Roots (求原根个数)

Primitive Roots 题目链接:http://poj.org/problem?id=1284 利用定理:素数 P 的原根的个数为euler(p - 1) typedef long long ll; using namespace std; /* 求原根 g^d ≡ 1(mod p) 其中d最小为p-1,g 便是一个原根 复杂度:O(m)*log(P-1)(m为p-1的质因子个数) */ ll euler(ll x) { ll res = x; for (ll i = 2; i <= x

poj 1284 Primitive Roots 【原根】【数论】

题目链接 :传送门 题目大意: 求一个质数的原根个数. 先普及一下原根的定义: 设m是正整数,a是整数,若a模m的阶等于euler(m),则称a为模m的一个原根. eg: m=7,euler(7) =  6(1,2,3,4,5,6) 则: 1   1^(n)mod7=1! = 6 2   2^(n)mod7={2 4 1}!=6 3   3^(n)mod7={3,2,6,4,5,1}==6   故3是模7的原根 4   4^(n)mod7={4,2,1}!=6 5   5^(n)mod7={5,

poj 1284 Primitive Roots 求素数元根数

题意: 给奇素数p,求p有多少原根. 分析: phi(p-1),数论有具体证明. 代码: //poj 1284 #include <iostream> using namespace std; int main() { int n; while(scanf("%d",&n)==1){ --n; int ans=n; for(int i=2;i*i<=n;++i) if(n%i==0){ ans-=ans/i; while(n%i==0) n/=i; } if(

POJ 1284 Primitive Roots (原根)

Primitive Roots Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3219   Accepted: 1858 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 eq

POJ 1284 Primitive Roots

Primitive Roots Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5481   Accepted: 3101 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 eq

原根二连 HDU 4992 &amp;&amp; poj 1284 Primitive Roots

原根存在的充要条件 n = 1,2,4,p^r (p为奇素数,r为任意正整数) 原根的性质 若n存在原根,则原根个数为φ(φ(n)) 若g是n的一个原根,则g^d是n的原根的充要条件为gcd(d,φ(n)) = 1 一个数的全体原根乘积模n余1 一个数的全体原根和模n余μ(n-1) #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using names

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

poj 1284 Primitive Roots(原根)

定理:假如一个数x有原根,则元根的个数为phi(phi(x)),phi(x)为小于x且与x互质的正整数个数. #include <iostream> #include <cmath> using namespace std; int p; int f(int x){ int ans=x; int m=sqrt(x+0.5); for(int i=2;i<=m;i++)if(x%i==0){ ans=ans/i*(i-1); while(x%i==0)x/=i; } if(x&

【POJ】1284 Primitive Roots

http://poj.org/problem?id=1284 题意:求一个素数p的原根个数.(p<=65535) #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <iostream> using namespace std; const int lim=65535, N=70005; int p[N], pcnt, np[