51nod 1135 原根(原根)

题意

题目链接

Sol

可以证明素数的原根不会超过他的\(\frac{1}{4}\)

那么预处理出\(P - 1\)的所有的质因数\(p_1, p_2 \dots p_k\),暴力判断一下,如果$\exists i, a^{\frac{P - 1}{p_i}} \equiv 1 \pmod {P - 1} $

那么说明\(a\)不是\(P\)的原根,因为根据原根的定义,需要保证\(P-1\)是第一个满足\(a^{P - 1} \equiv 1 \pmod {P - 1}\)的数

#include<cstdio>
using namespace std;
const int MAXN = 1e6 + 10;
int fp(int a, int p, int mod) {
    int base = 1;
    while(p) {
        if(p & 1) base = 1ll * base * a % mod; 
        a = 1ll * a * a % mod; p >>= 1;
    }
    return base;
}
int GetG(int x) {
    static int q[MAXN]; int tot = 0, tp = x - 1;
    for(int i = 2; i * i <= tp; i++) {//这里是i * i 
        if(!(tp % i)) {
            q[++tot] = i;
            while(!(tp % i)) tp /= i;
        }
    }
    if(tp > 1) q[++tot] = tp;
    for(int i = 2, j; i <= x - 1; i++) {
        for(j = 1; j <= tot; j++) if(fp(i, (x - 1) / q[j], x) == 1) break;
        if(j == tot + 1) return i;
    }
}
int main() {
    int P;
    scanf("%d", &P);
    printf("%d", GetG(P));
    return 0;
}
/*
1000000007
*/
posted @ 2018-11-27 16:29  自为风月马前卒  阅读(417)  评论(0编辑  收藏  举报

Contact with me