Rubin-Miller与Pollard Rho
两个没什么卵用的算法。
只放一下模板:
//BZOJ 3667
//by Cydiater
//2017.2.20
#include <iostream>
#include <queue>
#include <map>
#include <ctime>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <bitset>
#include <set>
#include <vector>
#include <complex>
using namespace std;
#define ll long long
#define up(i,j,n) for(ll i=j;i<=n;i++)
#define down(i,j,n) for(ll i=j;i>=n;i--)
#define cmax(a,b) a=max(a,b)
#define cmin(a,b) a=min(a,b)
inline ll read(){
char ch=getchar();ll x=0,f=1;
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
ll N,ans=0,a[]={2,3,5,7,11,13,17,19,23,29};
namespace solution{
ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);}
ll mul(ll x,ll y,ll mod){
ll tmp=0;
while(y){
if(y&1)(tmp+=x)%=mod;
(x+=x)%=mod;y>>=1;
}
return tmp;
}
ll quick_pow(ll base,ll ind,ll mod){
ll tmp=1;
while(ind){
if(ind&1)tmp=mul(tmp,base,mod);
base=mul(base,base,mod);ind>>=1;
}
return tmp;
}
bool OK(ll base,ll num){
if(!(num&1)||num==1)return 0;
ll ind=num-1;
while(!(ind&1))ind>>=1;
ll Num=quick_pow(base,ind,num);
if(Num==1)return 1;
while(ind<num){
if(Num==num-1)return 1;
Num=mul(Num,Num,num);ind<<=1;
}
return 0;
}
bool isPrime(ll num){
up(i,0,9){
if(num==a[i])return 1;
if(!OK(a[i],num))return 0;
}
return 1;
}
ll rho(ll num,ll c){
ll i=1,k=2,x=rand()%(num-1)+1,y=x;
while(true){
i++;
x=(mul(x,x,num)+c)%num;
ll d=gcd((y-x+num)%num,num);
if(d>1&&d<num)return d;
if(x==y)return num;
if(i==k){y=x;k<<=1;}
}
}
void find(ll num,ll k){
if(num==1||num<=ans)return;
if(isPrime(num)){
cmax(ans,num);
return;
}
ll p=num,c=k;
while(p>=num)p=rho(num,k--);
find(p,c);find(num/p,c);
}
void Solve(){
N=read();ans=0;
while(N--){
ll num=read();ans=0;
find(num,120);
if(ans==num)puts("Prime");
else printf("%lld\n",ans);
}
}
int main(){
freopen("input.in","r",stdin);
using namespace solution;
Solve();
return 0;
}
Rubin-Miller与Pollard Rho的更多相关文章
- Pollard rho算法+Miller Rabin算法 BZOJ 3668 Rabin-Miller算法
BZOJ 3667: Rabin-Miller算法 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 1044 Solved: 322[Submit][ ...
- Miller Rabin素数检测与Pollard Rho算法
一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...
- Miller-Rabin 素性测试 与 Pollard Rho 大整数分解
\(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...
- 浅谈 Miller-Robbin 与 Pollard Rho
前言 $Miller-Robbin$ 与 $Pollard Rho$ 虽然都是随机算法,不过用起来是真的爽. $Miller Rabin$ 算法是一种高效的质数判断方法.虽然是一种不确定的质数判断法, ...
- Pollard Rho 算法简介
\(\text{update 2019.8.18}\) 由于本人将大部分精力花在了cnblogs上,而不是洛谷博客,评论区提出的一些问题直到今天才解决. 下面给出的Pollard Rho函数已给出散点 ...
- Pollard Rho算法浅谈
Pollard Rho介绍 Pollard Rho算法是Pollard[1]在1975年[2]发明的一种将大整数因数分解的算法 其中Pollard来源于发明者Pollard的姓,Rho则来自内部伪随机 ...
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- 整数(质因子)分解(Pollard rho大整数分解)
整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...
- Pollard Rho因子分解算法
有一类问题,要求我们将一个正整数x,分解为两个非平凡因子(平凡因子为1与x)的乘积x=ab. 显然我们需要先检测x是否为素数(如果是素数将无解),可以使用Miller-Rabin算法来进行测试. Po ...
- 初学Pollard Rho算法
前言 \(Pollard\ Rho\)是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:\(MillerRabin\)素数测试(关于\(MillerRabin\),可以参考这篇博客:初学Mi ...
随机推荐
- strut2的标签
DIY部落 新闻中心 交流论坛 千寻搜索 点击浏览该栏目下的更多电子书 收藏本站 struts2标签详解 文章整理: www.diybl.com 文章来源: 网络 去论坛 建我的b ...
- disk_free_space
$df = disk_free_space('/')/1024/1024/1024; $df_c = disk_free_space("c:"); $df_d = disk_fre ...
- kerberos (https://en.wikipedia.org/wiki/Kerberos_(protocol))
Protocol[edit] Description[edit] The client authenticates itself to the Authentication Server (AS) w ...
- gateio API
本文介绍gate io API 所有交易对 API 返回所有系统支持的交易对 URL: https://data.gateio.io/api2/1/pairs 示例: # Request GET: h ...
- PHP使用 zip 扩展压缩文件
在公司遇到一个问题,是使用zip打包用户的上传文件,提供集体下载. -- 第一个想法就是使用exec在Linux进行打包.但是...exec方法吧,你懂得,我不太愿意使用这个函数. -- 于是上网查找 ...
- Oracle与Sql server 在SQL上的不同
Oracle与Sql server都遵循SQL-92标准:http://owen.sj.ca.us/rkowen/howto/sql92F.html,但是也有一些不同之处,差别如下: Oracle中表 ...
- python中operator.itemgetter函数
operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. k = [,,] b = ) print(b(k)) #输 ...
- 四、Mosquitto 高级应用之用户配置
本文将讲解 Mosquitto 用户机制.如果还没有搭建 Mosquitto 服务的可以参考我的另外两篇文章<< 一.Mosquitto 介绍&安装>> << ...
- python排序函数sort()与sorted()区别
sort是容器的函数:sort(cmp=None, key=None, reverse=False) sorted是python的内建函数:sorted(iterable, cmp=None, key ...
- MySQL读写分离-简单思考
本文图片资源均来自互联网,没有干货,只是提供一种简单的思路. 基础原理 两台MySQL机器一个主,一个从实现数据实时同步比较简单,代码层面无需任何修改,添加一台机器简单配置配置即可,但是MySQL数据 ...