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 ...
随机推荐
- 【转】JavaScript prototype
原文地址:http://www.cnblogs.com/dolphinX/p/3286177.html 用过JavaScript的同学们肯定都对prototype如雷贯耳,但是这究竟是个什么东西却让初 ...
- codeforces 592B/C
题目链接:http://codeforces.com/contest/592/problem/B B. The Monster and the Squirrel time limit per test ...
- SecureFX 中文乱码
1. 找到SecureFX配置文件夹(选项–全局选项,常规下的配置文件夹),比如:C:\Users\James\AppData\Roaming\VanDyke\Config\Sessions 2. ...
- physics---hdu5826(积分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5826 题意:有n个小球在一条直线上滚动,起始位置为xi, 方向为di(-1往左走,1往右走),初始速度 ...
- 自定义vueawesomeswiper分页器样式
swiperOption: {//swiper的配置项 notNextTick: true,//想获得swiper实例对象,这个必须为true direction: 'vertical', // gr ...
- Unity-反编译由IL生成的DLL文件
本文由博主SunboyL原创,转载请注明出处:http://www.cnblogs.com/xsln/p/DLL_DeCompilation.html 在Unity实际开发过程中,我们 ...
- 新建的web工程找不到javax.servlet.http.httpservlet
1.在出现此错误的项目上右键-->Build Path --> Configure Build Path 2.点击右边的 ADD Library 3.选中“Server Runtime”, ...
- 分布式网格缓存Coherence简介
Coherence企业级缓存(一) 特点 摘要:Oracle Coherence是一个企业级的分布式集群缓存框架.具有自管理,自恢复,高可用性,高扩展性等优良特点,在电信BOSS等项目中有很大的应用价 ...
- druid.io本地集群搭建 / 扩展集群搭建
druid.io 是一个比较重型的数据库查询系统,分为5种节点 . 在此就不对数据库进行介绍了,如果有疑问请参考白皮书: http://pan.baidu.com/s/1eSFlIJS 单台机器的集群 ...
- HTTP头部信息解释分析(详细整理)
这篇文章为大家介绍了HTTP头部信息,中英文对比分析,还是比较全面的,若大家在使用过程中遇到不了解的,可以适当参考下 HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/ ...