其他分享
首页 > 其他分享> > dls的数论-阶与原根,指数方程

dls的数论-阶与原根,指数方程

作者:互联网



#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
vector<int> pf;

LL qmi(LL a, LL b, LL mod){
    LL res = 1 % mod;
    while(b){
        if(b&1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

int main(){
    int p, T; scanf("%d %d", &p, &T);
    int m = p - 1;
    for(int i = 2; i <= m / i; i ++){
        if(m % i == 0){
            pf.push_back(i);
            while(m % i == 0) m /= i;
        }
    }
    if(m != 1) pf.push_back(m);
    while(T--){
        int x; scanf("%d", &x);
        int res = p - 1;
        for(auto pp : pf){
            while(res % pp == 0 && qmi(x, res / pp, p) == 1) res /= pp;
        }
        printf("%d\n", res);
    }    
    return 0;
}

原根

循环节的长度是f(m),1-m之间与m互质的数是f(m)个,这些数是一样
g是一个原根,g^i 1-f(m)能表示上面所有的数

标签:原根,int,res,LL,阶与,dls,long,mod
来源: https://www.cnblogs.com/njw1123/p/16100833.html