poj1284 Primitive Roots

来源:互联网 发布:理财软件哪款靠谱 编辑:程序博客网 时间:2024/05/17 22:19

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
233179
Sample Output
10824



求原根的。

哪些数有原根?

n = 1,2,4,p^r,2p^r。期中p为奇素数,r为任意的正整数。

原根的一些性质:

•一个数n如果有原根,那么有phi(phi(n))个
•高斯证明了:
•一个数n的全体原根乘积模n余1
•一个数n的全体原根总和模n余μ(n-1)(莫比乌斯函数)

#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int MAXN = 70000;int prim[MAXN],phi[MAXN];bool vis[MAXN];int tot;void get_phi(){    phi[1] = 1;    tot = 0;    for(int i = 2; i < MAXN; ++i)    {        if(!vis[i])        {            prim[tot++] = i;            phi[i] = i-1;        }        for(int j = 0; j < tot; ++j)        {            if(prim[j]*i > MAXN)break;            vis[i*prim[j]] = 1;            if(i%prim[j] == 0)            {                phi[i*prim[j]] = phi[i] * prim[j];                break;            }            else phi[i*prim[j]] = phi[i]*phi[prim[j]];        }    }}int main(){    int n;    get_phi();    while(~scanf("%d",&n))    {        printf("%d\n",phi[phi[n]]);    }    return 0;}