poj_1284_Primitive root

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

求模素数原根的方法:素因子分解,即的标准分解式,若恒有


          


成立,则就是的原根。(对于合数求原根,只需把换成即可)

 

定义:,使得成立的最小的,称为对模的阶,记为


定理:如果模有原根,那么它一共有个原根。


定理:,则


定理:如果为素数,那么素数一定存在原根,并且模的原根的个数为

 


定理:是正整数,是整数,若的阶等于,则称为模的一个原根。


   假设一个数对于模来说是原根,那么的结果两两不同,且有,那么可以称为是模的一个原根,归根到底就是当且仅当指数为的时候成立。(这里是素数)

 

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#define maxn 65550
using namespace std;
typedef long long ll;
int phi[maxn];
int a[maxn];
bool vis[maxn];
int val[maxn];
int prime[maxn],pn=0;
int main()
{
    int i,j;
    for(i=1; i<=maxn; i++)
        phi[i]=i;
    for(i=2; i<=maxn; i+=2)
        phi[i]/=2;
    for(i=3; i<=maxn; i+=2)
        if(phi[i]==i)
        {
            for(j=i; j<=maxn; j+=i)
                phi[j]=phi[j]/i*(i-1);
        }
    int n;
    while(~scanf("%d",&n))
    {
        cout<<phi[n-1]<<endl;
    }
}

  

posted @ 2017-11-14 15:49  会飞的雅蠛蝶  阅读(535)  评论(0编辑  收藏  举报