求一个数的最小原根

暴力枚举+判断

#include<bits/stdc++.h>
using namespace std;
#define rint register int
int cnt,fac[1000010];
const int P=1004423491;
void getfac(int x) {
    for(rint i=2,mx=sqrt(x);i<=mx;++i) {
        if(x%i==0) {
            fac[++cnt]=i;
            while(x%i==0)x/=i;
        }
    }
}
int qpow(int n,int k) {
    int res=1;
    while(k) {
        if(k&1)res=1ll*res*n%P;
        n=1ll*n*n%P;
        k>>=1;
    }
    return res;
}
bool check(int x) {
    for(rint i=1;i<=cnt;++i) {
        if(qpow(x,(P-1)/fac[i])==1)return 0;
    }
    return 1;
}
int main() {
    getfac(P-1);
    for(rint i=2;i<P;++i) {
        if(check(i)) {
            printf("%d\n",i);
            return 0;
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/zzctommy/p/12537369.html