POJ

Problem  POJ - 1284- Primitive Roots

Time Limit: 1000 mSec

Problem Description

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

题解:考察原根的性质,首先给出原根存在的充要条件:

  N = 1, 2, 4, p^a, 2p^a

这里的n是奇素数,所以原根必定存在,再有定理:

  若n得原根存在,则n得原根个数为phi(phi(n))

百度到一个关于奇素数情况的证明,在此mark一下:

对于给出的素数p,
首先要明确一点:p的元根必然是存在的(这一点已由Euler证明,此处不再赘述),因此,不妨设其中的一个元根是a0(1<=a0<=p-1)
按照题目的定义,a0^i(1<=i<=p-1) mod p的值是各不相同的,再由p是素数,联系Fermat小定理可知:q^(p-1) mod p=1;(1<=q<=p-1)(这个在下面有用)
下面证明,如果b是p的一个异于a的元根,不妨令b与a0^t关于p同余,那么必然有gcd(t,p-1)=1,亦即t与p-1互质;反之亦然;
证明:
若d=gcd(t,p-1)>1,令t=k1*d,p-1=k2*d,则由Fermat可知
(a0^(k1*d))^k2 mod p=(a0^(k2*d))^(k1) mod p=(a0^(p-1))^(k1) mod p=1
再由b=a0^t (mod p),结合上面的式子可知:
(a0^(k1*d))^k2 mod n=b^k2 mod p=1;
然而b^0 mod p=1,所以b^0=b^k2 (mod p),所以b^i mod p的循环节=k2<p-1,因此这样的b不是元根;

再证,若d=gcd(t,p-1)=1,即t与p-1互质,那么b必然是元根;
否则假设存在1<=j<i<=p-1,使得b^j=b^i (mod p),即a0^(j*t)=a0^(i*t) (mod p),由a0是元根,即a0的循环节长度是(p-1)可知,(p-1) | (i*t-j*t)->(p-1) | t*(i-j),由于p与
t互质,所以(p-1) | (i-j),但是根据假设,0<i-j<p-1,得出矛盾,结论得证;

由上面的两个证明可知b=a0^t (mod p),是一个元根的充要条件是t与p-1互质,所有的这些t的总个数就是Phi(p-1);

 1 //#include <bits/stdc++.h>
 2 #include <iostream>
 3 #include <cmath>
 4 
 5 using namespace std;
 6 
 7 #define REP(i, n) for (int i = 1; i <= (n); i++)
 8 #define sqr(x) ((x) * (x))
 9 
10 const int maxn = 10;
11 const int maxm = 200000 + 10;
12 const int maxs = 52;
13 
14 typedef long long LL;
15 //typedef pair<int, int> pii;
16 //typedef pair<double, double> pdd;
17 
18 const LL unit = 1LL;
19 const int INF = 0x3f3f3f3f;
20 const LL Inf = 0x3f3f3f3f3f3f3f3f;
21 const double eps = 1e-14;
22 const double inf = 1e15;
23 const double pi = acos(-1.0);
24 const LL mod = 1000000007;
25 
26 LL get_phi(LL n)
27 {
28     LL ans = n;
29     LL m = sqrt(n + 0.5);
30     for (LL i = 2; i <= m; i++)
31     {
32         if (n % i == 0)
33         {
34             while (n % i == 0)
35             {
36                 n /= i;
37             }
38             ans = ans * (i - 1) / i;
39         }
40     }
41     if (n > 1)
42         ans = ans * (n - 1) / n;
43     return ans;
44 }
45 
46 int main()
47 {
48     ios::sync_with_stdio(false);
49     cin.tie(0);
50     //freopen("input.txt", "r", stdin);
51     //freopen("output.txt", "w", stdout);
52     LL p;
53     while (cin >> p)
54     {
55         cout << get_phi(p - 1) << endl;
56     }
57     return 0;
58 }
 
原文地址:https://www.cnblogs.com/npugen/p/10835707.html