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是一个素 ...
随机推荐
- Ubuntu14.04+caffe+CPU
刚刚在上篇博客记录了windows10下GPU版本caffe的安装,正准备跑跑论文里的代码,发现好多命令都是.sh命令,这是linux系统的脚本文件.不能直接在windows下运行,于是我想把.sh转 ...
- ES系列十、ES常用查询API
1.term查询 { "query": { "term": { "title": "crime" } } } 1.1.指 ...
- springboot系列二、springboot项目搭建
一.官网快速构建 1.maven构建项目 1.访问http://start.spring.io/ 2.选择构建工具Maven Project.Spring Boot版本2.1.1以及一些工程基本信息, ...
- ODOO引用Echarts数据展示
作为一个后端开发,首先想到的是将需要的数据进行处理反馈给前端. 具体如下: 然后就是专门的echarts模块(我这样写主要是因为echarts会用到的地方比较多,后续直接调用) 1. 2.echart ...
- 关于php-fpm方式和apache配合使用的几点记录
1.apache2.4以后可以编译单独的模块可以使用fastcgi和phpfpm进行配合,打开以下的模块即可 LoadModule proxy_module modules/mod_proxy.so ...
- Day6------------磁盘用满的两种情况
1.文件包含元数据和写入的内容 元数据:存在硬盘中的inode ls -i /etc/passwd.bak 查看inode df -i 查看inode 2.磁盘用满的两种情况 1).内容太多 2).空 ...
- Python-CSS入门
一.架构分析 页面 => div的层级结构 => 具有采用哪些功能标签显示内容 结构层 > 位置层(布局层) > 内容层 二.css引入 - 行间式 <!-- 简单直接, ...
- 在chrome开发者工具中观察函数调用栈、作用域链与闭包
在chrome开发者工具中观察函数调用栈.作用域链与闭包 在chrome的开发者工具中,通过断点调试,我们能够非常方便的一步一步的观察JavaScript的执行过程,直观感知函数调用栈,作用域链,变量 ...
- Annoy 近邻算法
Annoy 随机选择两个点,以这两个节点为初始中心节点,执行聚类数为2的kmeans过程,最终产生收敛后两个聚类中心点 二叉树底层是叶子节点记录原始数据节点,其他中间节点记录的是分割超平面的信息 但是 ...
- Linux下配置自动更新时间
1,修正本地时区及ntp服务 [root@VM_0_13_centos ~]# yum -y install ntp [root@VM_0_13_centos ~]# rm -rf /etc/loca ...