zoukankan      html  css  js  c++  java
  • 快速求原根

    当需要求质数(P)的原根(G),只需枚举(a in [2,P - 1]),检验对(P - 1)的所有质因子(p_i)(a^{frac{P - 1}{p_i}} mod P)是否等于(1),若都不等于(1),则(a)(P)的原根

    51Nod原根

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<map>
    #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
    #define REP(i,n) for (int i = 1; i <= (n); i++)
    #define mp(a,b) make_pair<int,int>(a,b)
    #define cls(s) memset(s,0,sizeof(s))
    #define cp pair<int,int>
    #define LL long long int
    using namespace std;
    const int maxn = 100005,maxm = 100005,INF = 1000000000;
    inline int read(){
    	int out = 0,flag = 1; char c = getchar();
    	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    	return out * flag;
    }
    inline int qpow(int a,LL b,int pr){
    	int re = 1;
    	for (; b; b >>= 1,a = 1ll * a * a % pr)
    		if (b & 1) re = 1ll * re * a % pr;
    	return re;
    }
    int P,fac[maxn],cnt;
    void Sp(int x){
    	for (int i = 2; i * i <= x; i++)
    		if (x % i == 0){
    			fac[++cnt] = i;
    			while (x % i == 0) x /= i;
    		}
    	if (x - 1) fac[++cnt] = x;
    }
    void work(){
    	for (int i = 2; i < P; i++){
    		int flag = true;
    		for (int j = 1; j <= cnt; j++)
    			if (qpow(i,(P - 1) / fac[j],P) == 1){
    				flag = false;
    				break;
    			}
    		if (flag){
    			printf("%d
    ",i);
    			break;
    		}
    	}
    }
    int main(){
    	P = read();
    	Sp(P - 1);
    	work();
    	return 0;
    }
    
    
  • 相关阅读:
    [Luogu] P1886 滑动窗口
    [Luogu] P1195 口袋的天空
    [Luogu] P1331 海战
    [Luogu] P3952 时间复杂度
    运营活动如何防刷
    考研政治刷题小程序
    考研刷题小程序
    在线答题活动小程序
    知识竞答小程序v2.0
    知识竞答小程序
  • 原文地址:https://www.cnblogs.com/Mychael/p/9218871.html
Copyright © 2011-2022 走看看