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;
    }
    
    
  • 相关阅读:
    JavaScript&DOM
    avalon.js的循环操作在表格中的应用
    merge()
    建立表空间以及用户
    SSI框架下,用jxl实现导出功能
    SQL递归查询实现组织机构树
    vue+webpack实践
    使用canvas绘制一片星空
    在canvas中使用html元素
    CSS3-transform 转换/变换
  • 原文地址:https://www.cnblogs.com/Mychael/p/9218871.html
Copyright © 2011-2022 走看看