Pollard's Rho算法简单总结
先贴一份代码在这。
最近几天实在是太忙了没时间更新了。
代码
#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算法简单总结的更多相关文章
- Miller-Rabin素性测试|Pollard's Rho算法
Miller-Rabin 素性测试 Miller-Rabin 素数测试 一本通上的M-R不透彻啊~ Miller-Rabin是利用随机化算法判断一个数是合数还是素数. 首先,如果一个数N是素数,那么他 ...
- 【快速因数分解】Pollard's Rho 算法
Pollard-Rho 是一个很神奇的算法,用于在 $O(n^{\frac{1}4}) $的期望时间复杂度内计算合数 n 的某个非平凡因子(除了1和它本身以外能整除它的数).事书上给出的复杂度是 \( ...
- Pollard's rho algorithm和涉及到的两个循环检测算法
0. 简单介绍 Pollard的\(\rho\)算法是John Pollard在1975年发明的,用于分解质因数[1].假定被分解的数为N,N的最小的质因数为\(p(p\ne N)\),那么该算法可以 ...
- 数学基础IV 欧拉函数 Miller Rabin Pollard's rho 欧拉定理 行列式
找了一些曾经没提到的算法.这应该是数学基础系最后一篇. 曾经的文章: 数学基础I 莫比乌斯反演I 莫比乌斯反演II 数学基础II 生成函数 数学基础III 博弈论 容斥原理(hidden) 线性基(h ...
- Pollard Rho 算法简介
\(\text{update 2019.8.18}\) 由于本人将大部分精力花在了cnblogs上,而不是洛谷博客,评论区提出的一些问题直到今天才解决. 下面给出的Pollard Rho函数已给出散点 ...
- Pollard Rho算法浅谈
Pollard Rho介绍 Pollard Rho算法是Pollard[1]在1975年[2]发明的一种将大整数因数分解的算法 其中Pollard来源于发明者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][ ...
- 初学Pollard Rho算法
前言 \(Pollard\ Rho\)是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:\(MillerRabin\)素数测试(关于\(MillerRabin\),可以参考这篇博客:初学Mi ...
- Miller Rabin素数检测与Pollard Rho算法
一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...
随机推荐
- [Python3] 026 常用模块 calendar
目录 calendar 1. calendar.calendar(year, w, l, c, m) 2. calendar.prcal(year, w, l, c, m) 3. calendar.m ...
- http请求跨域问题分析
http请求跨域问题分析 个人认为可能涉及很多http的知识,本人才疏学浅不敢妄自揣测,只是做个笔记为了以后对比理解,从原生fetch说起吧,前提假设有个后端服务,我用express来模拟,如下: v ...
- 手撕ES6--Promise
手撕ES6--Promise:https://www.jianshu.com/p/0925eae38d2c 手写一个Promise,附源码分析:https://blog.csdn.net/weixin ...
- MySQL的日志系统
一.日志类型 逻辑日志:存储了逻辑SQL修改语句 物理日志:存储了数据被修改的值 二.binlog 1.定义 binlog 是 MySQL 的逻辑日志,也叫二进制日志.归档日志,由 MySQL Ser ...
- “laravel.log” could not be opened: failed to open stream
百度了一下,说是要赋权限,按照操作赋了权限也还是报错,其实只要执行第一个就好,但为了保险起见,我都执行了,还是不行 chmod -R /storage chmod -R /storage/logs c ...
- 查找元素在list中的位置以及折半查询
问题 查找某个值在list中的位置 解决思路 能够用折半查询的方法解决此问题. 解决(Python) #! /usr/bin/env python #coding:utf-8 #折半查找某个元素在li ...
- mybatis中foreach的用法以及特殊的情况的用法
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...
- RabbitMQ入门教程(五):扇形交换机发布/订阅(Publish/Subscribe)
原文:RabbitMQ入门教程(五):扇形交换机发布/订阅(Publish/Subscribe) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. ...
- group_concat默认长度限制
这几天做后台一个订单汇总数据报表时,发现当使用group_concat函数时,发现会漏掉数据,究其原因是因为这个函数有默认长度显示1024 可以修改mysql配置文件my.ini 设置group_co ...
- Jquery复习(一)之animate()易忘点
可以用 animate() 方法来操作所有 CSS 属性吗? 是的,几乎可以!不过,需要记住一件重要的事情:当使用 animate() 时,必须使用 Camel 标记法书写所有的属性名,比如,必须使用 ...