P4718 【模板】Pollard-Rho算法
题面
题解
太神仙了学不来orz
//minamoto
#include<bits/stdc++.h>
#define R register
#define ll long long
#define dd long double
#define fp(i,a,b) for(int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
const int base[]={2,3,7,61,24251};
inline ll mul(R ll x,R ll y,R ll P){R ll k=(dd)x*y/P;k=x*y-k*P;return k<0?k+P:k;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
inline ll g(R ll x,R ll n,R ll c){x=mul(x,x,n)+c;return x>n?x-n:x;}
inline ll Abs(R ll x){return x<0?-x:x;}
ll ksm(R ll x,R ll y,R ll P){
R ll res=1;
for(;y;y>>=1,x=mul(x,x,P))if(y&1)res=mul(res,x,P);
return res;
}
bool miller(ll x){
if(x<2||x==46856248255981ll)return false;
if(x==2||x==3||x==7||x==61||x==24251)return true;
if(!(x&1)||!(x%3)||!(x%61)||!(x%24251))return false;
ll p=x-1;int t=0,j;
while(!(p&1))p>>=1,++t;
fp(i,0,4){
if(base[i]>x)break;
ll res=ksm(base[i],p,x);
if(res==1||res==x-1)continue;
for(j=1;j<=t;++j){
res=mul(res,res,x);
if(res==x-1)break;
}
if(j>t)return false;
}
return true;
}
const int M=(1<<7)-1;
ll rho(ll n){
if(!(n&1))return 2;if(!(n%3))return 3;
ll x=0,y=x,t=1,q=1,c=rand()%(n-1)+1;
for(R int k=2;;k<<=1,y=x,q=1){
fp(i,1,k){
x=g(x,n,c);
q=mul(q,Abs(x-y),n);
if(!(i&M)){
t=gcd(q,n);
if(t>1)break;
}
}
if(t>1||(t=gcd(q,n))>1)break;
}
return t;
}
ll res;
void find(ll x){
if(x==1||x<=res)return;
if(miller(x))return res=x,void();
ll p=x;
while(p==x)p=rho(x);
while(x%p==0)x/=p;
find(p),find(x);
}
int main(){
srand(time(0));
// freopen("testdata.in","r",stdin);
int T;ll n;scanf("%d",&T);
while(T--){
scanf("%lld",&n),res=0,find(n);
res==n?printf("Prime\n"):printf("%d\n",res);
}
return 0;
}
P4718 【模板】Pollard-Rho算法的更多相关文章
- Pollard Rho算法浅谈
Pollard Rho介绍 Pollard Rho算法是Pollard[1]在1975年[2]发明的一种将大整数因数分解的算法 其中Pollard来源于发明者Pollard的姓,Rho则来自内部伪随机 ...
- Pollard Rho 算法简介
\(\text{update 2019.8.18}\) 由于本人将大部分精力花在了cnblogs上,而不是洛谷博客,评论区提出的一些问题直到今天才解决. 下面给出的Pollard Rho函数已给出散点 ...
- Miller Rabin素数检测与Pollard Rho算法
一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...
- Pollard rho算法+Miller Rabin算法 BZOJ 3668 Rabin-Miller算法
BZOJ 3667: Rabin-Miller算法 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 1044 Solved: 322[Submit][ ...
- 初学Pollard Rho算法
前言 \(Pollard\ Rho\)是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:\(MillerRabin\)素数测试(关于\(MillerRabin\),可以参考这篇博客:初学Mi ...
- 大整数分解质因数(Pollard rho算法)
#include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> ...
- BZOJ 5330 Luogu P4607 [SDOI2018]反回文串 (莫比乌斯反演、Pollard Rho算法)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=5330 (Luogu) https://www.luogu.org/prob ...
- 【快速因数分解】Pollard's Rho 算法
Pollard-Rho 是一个很神奇的算法,用于在 $O(n^{\frac{1}4}) $的期望时间复杂度内计算合数 n 的某个非平凡因子(除了1和它本身以外能整除它的数).事书上给出的复杂度是 \( ...
- Miller-Rabin 素性测试 与 Pollard Rho 大整数分解
\(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...
- Pollard Rho因子分解算法
有一类问题,要求我们将一个正整数x,分解为两个非平凡因子(平凡因子为1与x)的乘积x=ab. 显然我们需要先检测x是否为素数(如果是素数将无解),可以使用Miller-Rabin算法来进行测试. Po ...
随机推荐
- SQLite连接C#笔记
不得不吐槽,实在是太坑了.以下几点一定要注意: 要下载两个东西,都要上官网.一个是SQLite for Windows,一个是System.Data.SQLite. 下载下来的DLL里面有个test, ...
- Day1--Python基础1--下半部分
一..pyc是什么 1. Python是一门解释型语言? 我初学Python时,听到的关于Python的第一句话就是,Python是一门解释性语言,我就这样一直相信下去,直到发现了*.pyc文件的存在 ...
- linux下mysql配置文件my.cnf最详细解释
MySQL配置文件在Windows下叫my.ini,在MySQL的安装根目录下:在Linux下叫my.cnf,该文件位于/etc/my.cnf. 可以查找下:find / -name my.cnf m ...
- SQL属性第一个值不被选中,属性默认第一个值
把 Please Choose Color 属性名设置为不可选的 UPDATE `products_attributes` SET `attributes_display_only` = '1' WH ...
- php字符型转整型
$arr = array(0=>1,"aa"=>2, 3, 4); foreach($arr as $key=>$val){ print($key == &quo ...
- The R Project for Statistical Computing
[Home] Download CRAN R Project About R Contributors What’s New? Mailing Lists Bug Tracking Conferenc ...
- div的作用
<div></div>主要是用来设置涵盖一个区块为主,所谓的区块是包含一行以上的数据,所以在<div></div>的开始之前与结束后,浏览都会自动换行, ...
- myeclipse.ini
myeclipse10 32位 我的配置 #utf8 (do not remove) #utf8 (do not remove) -startup ../Common/plugins/org.ecli ...
- checked多选,取消,反选
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C++读入整行字符串的方法
string s; getline(cin,s); cout<<s<<endl; ]; scanf("%[^\n]%*c",s); printf(" ...