zoukankan      html  css  js  c++  java
  • POJ 1284 Primitive Roots

    Primitive Roots

    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,n-1)内的数i,(i^1,i^2,i^3,,,i^n-1)分别%n,正好是(1,2,3,,,n-1),可以不按顺序,求满足这个式子的i的个数
    思路:首先他说取余后正好是(1,n-1),n又是奇素数,这让我们想到其实这个就是n中所有与n互质的数,奇素数的欧拉数就是n-1,也就是组成的一个缩系,
    然后我们再看那个i^1,,,,i^n-1很像求原根的条件


    就是他这里是g^0开始到n-2次方为止,题目是g^1到n-1,那么我们可不可以进行一个转换呢
    g^0我们可以看作1,那么我们可以由欧拉定理得知 g^a(m)=1mod
    所以我们可以得知题目就是求一个原根
    而求原根数有一个公式 原根数=phi(phi(m))
    奇素数的欧拉数就是 奇素数-1 所以在本题中也可以改为 phi(m-1)
    #include<cstdio>
    #include<cmath>
    using namespace std;
    typedef long long ll;
    ll phi(ll n)
    {
        ll sum=n;
        for(int i=2;i*i<=n;i++)
        {
            if(n%i==0)
            {
                sum=sum-sum/i;
                do
                {
                    n/=i;
                }while(n%i==0);
            }
        }
        if(n>1) sum=sum-sum/n;
        return sum;
    }
    int main()
    {
        ll n;
        while(scanf("%lld",&n)!=EOF)
        {
            printf("%lld
    ",phi(phi(n)));
        }
    }
  • 相关阅读:
    歌词:青春
    转载:只输了五元钱
    极静之渊

    低学翁
    心灵死化,何时巨变?
    无题

    意念
    爱之切
  • 原文地址:https://www.cnblogs.com/Lis-/p/9387692.html
Copyright © 2011-2022 走看看