ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

模板:pollard_rho

2021-04-26 15:03:46  阅读:144  来源: 互联网

标签:return int ll t2 t1 pollard rho mul 模板


  • 需要注意mr筛的次数稍微有点大,尽量保证正确性,sd要赋初值。
ll mul(ll x, ll y, ll p){
	ll t=(x*y-(ll)((long double)x/p*y)*p)%p;
	if (t<0) return t+p; return t;
}
ull sd;ll rd(){sd^=sd>>13,sd^=sd<<7,sd^=sd>>23;return sd>>1;}
ll gcd(ll x,ll y){return (x%y==0)?y:gcd(y,x%y);}
namespace miller_rabin{
	const int a[6]={2,3,5,7,11,61};
	ll ksm(ll x,ll y,ll p){ll s=1;for(;y;y/=2,x=mul(x,x,p)) if (y&1) s=mul(s,x,p); return s;}
	ll p,q,c;
	int chk(ll x){
		if (ksm(x,p-1,p)!=1) return 1;
		x=ksm(x,q,p);
		if (x==p-1||x==1) return 0;
		int k=0;
		while (k<=c&&x!=p-1) k++,x=mul(x,x,p);
		if (k>c) return 1;
	}
	int mr(ll p0){
		p=p0; if (p==1||!(p&1)) return p==2;
		q=p-1,c=0;
		while (q&1) q>>=1,c++;
		for(int t=0;t<6;t++) if (chk((a[t]-1)%(p-1)+1)) return 0;
		for(int t=0;t<8;t++) if (chk(rd()%(p-1)+1)) return 0;
		return 1;
	}
}
using miller_rabin::mr;

void getd(ll P){
	if (P==1) return;
	if (mr(P)) {
		for(int i=1;i<=tot;i++) if (p[i]==P) {c[i]++;return;}
		tot++,p[tot]=P,c[tot]=1;
		return;
	}
	while (1){
		ll c=rd()%(P-1)+1,t1=rd()%(P-1)+1,t2=(mul(t1,t1,P-1)+c)%(P-1)+1,g=1;
		int cnt=0;
		while (t1!=t2){
			ll t=abs(t2-t1);
			g=mul(g,t,P);
			if (!g){
				ll d=gcd(t,P);
				getd(d),getd(P/d);
				return;
			}
			if (++cnt==127){	
				cnt=0;
				ll d=gcd(g,P);
				if (d>1) {getd(d),getd(P/d);return;}
			}
			t1=(mul(t1,t1,P-1)+c)%(P-1)+1;
			t2=(mul(t2,t2,P-1)+c)%(P-1)+1;
			t2=(mul(t2,t2,P-1)+c)%(P-1)+1;
		}
		ll d=gcd(g,P);
		if (d>1) {getd(d),getd(P/d);return;}
	}
}

标签:return,int,ll,t2,t1,pollard,rho,mul,模板
来源: https://blog.csdn.net/qq_43649416/article/details/116155975

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有