先贴一份代码在这。

最近几天实在是太忙了没时间更新了。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <ctime>
#define rin(i,a,b) for(int i=(a);i<=(b);i++)
#define rec(i,a,b) for(int i=(a);i>=(b);i--)
#define trav(i,a) for(int i=head[(a)];i;i=e[i].nxt)
typedef long long LL;
using std::cin;
using std::cout;
using std::endl; inline LL read(){
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
} LL n,ans,mi[6]={2,3,5,7,11,13}; inline LL gcd(LL x,LL y){
if(!x||!y) return x+y;
while(y){
std::swap(x,y);
y%=x;
}
return x;
} inline LL qpow(LL x,LL y,LL mod){
LL ret=1,tt=x%mod;
while(y){
if(y&1) ret=(__int128)ret*tt%mod;
tt=(__int128)tt*tt%mod;
y>>=1;
}
return ret;
} bool miller_rabin(LL num){
if(num<2) return 0;
if(num==2||num==3||num==5||num==7||num==11||num==13) return 1;
if(num%2==0||num%3==0||num%5==0||num%7==0||num%11==0||num%13==0) return 0;
LL temp=num-1;int cnt2=0;
while(!(temp&1)) temp>>=1,cnt2++;
rin(i,0,5){
LL now=qpow(mi[i],temp,num);
if(now==1||now==num-1) continue;
rin(j,1,cnt2){
now=(__int128)now*now%num;
if(now==num-1) break;
if(j==cnt2) return 0;
}
}
return 1;
} LL pollard_rho(LL num){
LL fixx=rand()%(num-1)+1,siz=2,x=fixx,fac=1;
LL b=rand()%(num-1)+1;
while(fac==1){
rin(i,1,siz){
x=((__int128)x*x+b)%num;
fac=gcd(std::abs(x-fixx),num);
if(fac>1) return fac;
}
siz<<=1;
fixx=x;
}
} void getdivisor(LL num){
if(num==1||num<=ans) return;
if(miller_rabin(num)){
ans=std::max(ans,num);
return;
}
LL x=num;
while(x==num) x=pollard_rho(x);
while(num%x==0) num/=x;
if(num<x) std::swap(num,x);
getdivisor(num);
getdivisor(x);
} int main(){
int T=read();
while(T--){
n=read();
ans=1;
getdivisor(n);
if(ans==n) printf("Prime\n");
else printf("%lld\n",ans);
}
return 0;
}

Pollard's Rho算法简单总结的更多相关文章

  1. Miller-Rabin素性测试|Pollard's Rho算法

    Miller-Rabin 素性测试 Miller-Rabin 素数测试 一本通上的M-R不透彻啊~ Miller-Rabin是利用随机化算法判断一个数是合数还是素数. 首先,如果一个数N是素数,那么他 ...

  2. 【快速因数分解】Pollard's Rho 算法

    Pollard-Rho 是一个很神奇的算法,用于在 $O(n^{\frac{1}4}) $的期望时间复杂度内计算合数 n 的某个非平凡因子(除了1和它本身以外能整除它的数).事书上给出的复杂度是 \( ...

  3. Pollard's rho algorithm和涉及到的两个循环检测算法

    0. 简单介绍 Pollard的\(\rho\)算法是John Pollard在1975年发明的,用于分解质因数[1].假定被分解的数为N,N的最小的质因数为\(p(p\ne N)\),那么该算法可以 ...

  4. 数学基础IV 欧拉函数 Miller Rabin Pollard's rho 欧拉定理 行列式

    找了一些曾经没提到的算法.这应该是数学基础系最后一篇. 曾经的文章: 数学基础I 莫比乌斯反演I 莫比乌斯反演II 数学基础II 生成函数 数学基础III 博弈论 容斥原理(hidden) 线性基(h ...

  5. Pollard Rho 算法简介

    \(\text{update 2019.8.18}\) 由于本人将大部分精力花在了cnblogs上,而不是洛谷博客,评论区提出的一些问题直到今天才解决. 下面给出的Pollard Rho函数已给出散点 ...

  6. Pollard Rho算法浅谈

    Pollard Rho介绍 Pollard Rho算法是Pollard[1]在1975年[2]发明的一种将大整数因数分解的算法 其中Pollard来源于发明者Pollard的姓,Rho则来自内部伪随机 ...

  7. Pollard rho算法+Miller Rabin算法 BZOJ 3668 Rabin-Miller算法

    BZOJ 3667: Rabin-Miller算法 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 1044  Solved: 322[Submit][ ...

  8. 初学Pollard Rho算法

    前言 \(Pollard\ Rho\)是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:\(MillerRabin\)素数测试(关于\(MillerRabin\),可以参考这篇博客:初学Mi ...

  9. Miller Rabin素数检测与Pollard Rho算法

    一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...

随机推荐

  1. [Python3] 031 常用模块 shutil & zipfile

    目录 shutil 1. shutil.copy() 2. shutil.copy2() 3. shutil.copyfile() 4. shutil.move() 5. 归档 5.1 shutil. ...

  2. [转帖]Ubuntu 18.04 server安装图形界面及realvnc远程桌面连接

    Ubuntu 18.04 server安装图形界面及realvnc远程桌面连接 https://blog.csdn.net/networken/article/details/88938304 转帖 ...

  3. 思考--PostgreSQL在与mysql的比较中稍微弱势项

    PostgreSQL在与mysql的比较中稍微弱势项: 1.都是堆表,没有所谓的聚集索引表,其实问题不大,聚集索引表也只是在使用聚集索引那些列有加速,而且pg也有聚集索引,只不过要定期重建. 2.mv ...

  4. A Dangerous Maze (期望值)

    https://vjudge.net/problem/LightOJ-1027?tdsourcetag=s_pctim_aiomsg [被满为姐姐碾压 降智打击题]

  5. Codeforces 1209F. Koala and Notebook

    传送门 考虑到达某个点时的数长度要尽量短,那么可以把边长看成此边十进制下的位数 那么对于最终答案我们只要考虑最短路 $DAG$ 上的情况 又发现其实边长都很小,所以可以暴力拆边,把边权都拆成 $1$, ...

  6. [Vue] vue的一些面试题4

    1.你知道 nextTick 的原理吗? 用法:在下次 DOM 更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法,获取更新后的 DOM. 异步更新队列提到 DOM 的更新是异步执行的,只要 ...

  7. Python 进阶篇

    作者:武沛齐 出处:http://www.cnblogs.com/wupeiqi/articles/5246483.html Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这 ...

  8. React Native 底部导航栏

    首先安装:npm install react-native-tab-navigator   然后再引入文件中    import TabNavigator from 'react-native-tab ...

  9. JS判断页面是否为浏览器当前页

    function currentPage() { var hiddenProperty = 'hidden' in document ? 'hidden' : 'webkitHidden' in do ...

  10. Linux Shell交互式自动化运维程序

    Expect是Linux操作系统下的一个用来处理交互操作,系统默认是没有安装expect库,所以执行expect会提示找不到命令,需要手动安装,其它安装也比较简单,可以通过二进制源码包编译配置进行安装 ...