BZOJ.3667.Rabin-Miller算法(MillerRabin PollardRho)
题目链接
Pollard_Rho:http://blog.csdn.net/thy_asdf/article/details/51347390
#include<cstdio>
#include<cctype>
#include<algorithm>
#define gc() getchar()
const int p[]={2,3,5,7,11,13,17,19};
typedef long long LL;
LL Ans;
inline LL read()
{
LL now=0,f=1;register char c=gc();
for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
}
inline LL Mult(LL a,LL b,LL p)//O(1)快速乘
{
LL tmp=a*b-(LL)((long double)a/p*b+1e-8)*p;
return tmp<0?tmp+p:tmp;
}
LL Fast_Pow(LL n,LL k,LL p)
{
LL t=1;
for(;k;k>>=1,n=n*n%p)
if(k&1) t=t*n%p;
return t;
}
bool Miller_Rabin(LL n)
{
if(n==2) return 1;
if(!(n&1)||n==1) return 0;
for(int i=0;i<8;++i)
if(n==p[i]) return 1;
else if(!(n%p[i])) return 0;
LL u=n-1,now,las; int t=0;
while(!(u&1)) u>>=1,++t;
for(int i=0;i<8;++i)
{
now=Fast_Pow(p[i],u,n);
for(int j=1;j<=t;++j)
{
las=now, now=Mult(now,now,n);
if(now==1&&las!=1&&las!=n-1) return 0;
}
if(now!=1) return 0;
}
return 1;
}
LL gcd(LL x,LL y)
{
return y?gcd(y,x%y):x;
}
LL Rho(LL n,LL delta)
{//现要分解n,有两个随机数x,y,若p=gcd(x-y,n)!=1&&p!=n,那么p为n的一个约数...省略
LL x=rand()%n,y=x,p=1; int k=2;//设定k为此次路径长
for(int i=1;p==1;++i)
{
x=(Mult(x,x,n)+delta)%n;//随机函数f(x)=x*x+d
p=gcd(std::abs(x-y),n);//多次生成随机数,直至找到p是n的一个因子
if(i==k) y=x,k<<=1;//达到k次后把y赋值为x。路径每次倍长
}
return p;
}
void Find(LL n)
{
if(n==1) return;
if(Miller_Rabin(n)) {Ans=std::max(Ans,n);/*fac[++cnt]=n;*/ return;}
LL t=n;
while(t==n) t=Rho(n,rand()%(n-1)+1);
//t=n说明这个随机函数会导致走到n的环上,再换一个重试即可
Find(t), Find(n/t);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("3667.in","r",stdin);
#endif
int t=read();LL n;
while(t--)
n=read(),Ans=0,Find(n),Ans==n?puts("Prime"):printf("%lld\n",Ans);
return 0;
}
BZOJ.3667.Rabin-Miller算法(MillerRabin PollardRho)的更多相关文章
- [Miller-Rabin & Pollard-rho]【学习笔记】
Miller-Rabin & Pollard-rho 很久之前就学过了...今天重学一遍 利用费马小定理,但不能判断伪素数的情况 基于a的伪素数n: \(a^{n-1} \equiv 1 \p ...
- 【模板】SPOJ FACT0 大数分解 miller-rabin & pollard-rho
http://www.spoj.com/problems/FACT0/en/ 给一个小于1e15的数,将他分解. miller-rabin & pollard-rho模板 #include & ...
- bzoj 3667: Rabin-Miller算法【Miller-Rabin】
Miller-Rabin模板 #include<iostream> #include<cstdio> #include<algorithm> using names ...
- BZOJ 3667 Pollard-rho &Miller-Rabin
论O(1)快速乘和O(logn)快速乘的差距-. //By SiriusRen #include <cstdio> #include <algorithm> using nam ...
- BZOJ 3667: Rabin-Miller算法 (Pollard-Rho 模板)
说实话,我知道每一步都干啥,但我完全不知道为啥这么做,也不知道为什么是正确的,反正会用就行了~ #include <cmath> #include <cstdio> #incl ...
- 【刷题】BZOJ 3667 Rabin-Miller算法
Input 第一行:CAS,代表数据组数(不大于350),以下CAS行,每行一个数字,保证在64位长整形范围内,并且没有负数.你需要对于每个数字:第一,检验是否是质数,是质数就输出Prime 第二,如 ...
- bzoj 3667 Rabin-Miller算法
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...
- 数论入门2——gcd,lcm,exGCD,欧拉定理,乘法逆元,(ex)CRT,(ex)BSGS,(ex)Lucas,原根,Miller-Rabin,Pollard-Rho
数论入门2 另一种类型的数论... GCD,LCM 定义\(gcd(a,b)\)为a和b的最大公约数,\(lcm(a,b)\)为a和b的最小公倍数,则有: 将a和b分解质因数为\(a=p1^{a1}p ...
- 梅森素数 判定总结 - Lucas-Lehmer算法 & Miller-rabin算法
梅森素数 定义: if m是一个正整数 and 2^m-1是一个素数 then m是素数 if m是一个正整数 and m是一个素数 then M(m)=2^m-1被称为第m个梅森数 if p是一个素 ...
随机推荐
- Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...
- NTFS文件系统简介
原文地址:http://www.cnblogs.com/watertao/archive/2011/11/28/2266595.html 1.简介 NTFS(New Technology File S ...
- 故障 -> nginx启动失败
描述:在用saltstack给 minion 安装 nginx 服务 时 提示 nginx 服务下载成功,但是启动失败. ---------- ID: nginx-systemctl Function ...
- poj1436水平可见线
还是线段树区间更新,这次不需要对线段离散化,但是要把线段纵坐标*2,可以举例模拟 #include<iostream> #include<cstring> #include&l ...
- intellij idea svn 修改文件后,父文件夹也标注修改
svn文件修改后,默认只有当前文件更改而父文件没有标注,很不直观:查了一顿后,发现,可以设置: File—->settings—->version control—–>勾选show ...
- python 全栈开发,Day50(Javascript简介,第一个JavaScript代码,数据类型,运算符,数据类型转换,流程控制,百度换肤,显示隐藏)
一.Javascript简介 Web前端有三层: HTML:从语义的角度,描述页面结构 CSS:从审美的角度,描述样式(美化页面) JavaScript:从交互的角度,描述行为(提升用户体验) Jav ...
- ruby学习-字符串
字符串 1.创建字符1:new用来创建新字符,empty?检验字符是否为空 title = String.new #=> "" title.empty? #=>true ...
- 设置Eclipse的类文件和xml文件代码自动补全
原文:https://blog.csdn.net/erlian1992/article/details/53706736 我们在平常编写代码的时候,不会记住大多数的类和文件的属性,方法等等,这就需要我 ...
- flex布局简介
一.概述 浮动在移动布局中不再重要,flex盒模型越来越重要. flexbox经历过三个版本,主要区别是2009年到2012年之间的语法变化. 最新的语法和现在规范是同步的(例display:flex ...
- python错误、调试、测试
1.错误, Python内置的try...except...finally用来处理错误十分方便.出错时,会分析错误信息并定位错误发生的代码位置才是最关键的. 程序也可以主动抛出错误,让调用者来处理相应 ...