zoukankan      html  css  js  c++  java
  • 【51NOD】1135 原根

    【题意】给定p,求p的原根g。3<=p<=10^9。

    【算法】数学

    【题解】p-1= p1^a1 * p2^a2 * pk^ak,g是p的原根当且仅当对于所有的pi满足g^[ (p-1)/pi ] ≠ 1 (%p)

    g一般很小,暴力求。

    #include<cstdio>
    #include<cmath>
    using namespace std;
    int p,b[1000],tot;
    int power(int x,int k){
        int ans=1;
        while(k){
            if(k&1)ans=1ll*ans*x%p;
            x=1ll*x*x%p;
            k>>=1;
        }
        return ans;
    }
    int main(){
        scanf("%d",&p);
        int sq=(int)(sqrt(p)+0.5),P=p-1;
        for(int i=2;i<=sq;i++)if(P!=1){
            if(P%i==0){
                b[++tot]=i;
                while(P%i==0)P/=i;
            }
        }
        if(P!=1)b[++tot]=P;
        for(int i=2;i<=p;i++){
            bool ok=1;
            for(int j=1;j<=tot;j++){
                if(power(i,(p-1)/b[j])==1){ok=0;break;}
            }
            if(ok){printf("%d",i);return 0;}
        }
        return 0;
    }
    View Code
  • 相关阅读:
    POJ
    POJ
    POJ
    POJ
    POJ
    ZOJ
    HDU
    python中主要存在的四种命名方式:
    python 中的 赋值 浅拷贝 深拷贝
    python中sorted方法和列表的sort方法使用
  • 原文地址:https://www.cnblogs.com/onioncyc/p/8459064.html
Copyright © 2011-2022 走看看