• 快速求原根


    当需要求质数(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;
    }
    
    
  • 相关阅读:
    记住密码 cookie+MD5 的应用[转]
    TDD测试[转]
    架构模式随笔
    MVC架构探究及其源码实现
    强大的SqlCacheDependency【转】
    使用Nant构建入门
    web架构设计经验分享[转]
    DIV+CSS布局大全
    如何突破Windows 2003 远程桌面连接数限制
    大型互联网网站架构心得[转]
  • 原文地址:https://www.cnblogs.com/Mychael/p/9218871.html
Copyright © 2020-2023  润新知