Miiler-Robin素数测试与Pollard-Rho大数分解法
Miiler-Robin素数测试
目前已知分解质因数以及检测质数确定性方法就只能\(sqrt{n}\)试除
但是我们可以基于大量测试的随机算法而有大把握说明一个数是质数
Miler-Robin素数测试基于以下两个原理:
费马小定理
即我们耳熟能详的
对于质数\(p\)
\]
二次探测原理
对于质数\(p\),如果存在\(x\)满足
\]
那么\(x\)只能是\(1\)或者\(p - 1\)
由此我们便可以随机生成多个\(x\),逐一用以上两个原理检验即可
只要全都符合,我们就有大概\(1 - (\frac{1}{4})^{T}\)的把握说\(p\)是一个质数
具体操作时,令\(p = 2^{t}r + 1\),我们对于\(z = 2^{m}r\),其中\(m\)小于等于\(t\),都使用二次探测原理检验
最后再利用费马小定理检验
bool Miller_Rabin(LL n){
if (n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13) return true;
if (n == 1 || n % 2 == 0 || n % 3 == 0 || n % 7 == 0 || n % 11 == 0 || n % 13 == 0) return false;
int T = 50;
LL t = n - 1,k = 0;
while (!(t & 1)) t >>= 1,k++;
while (T--){
LL x = qpow(random(n),t,n),y;
REP(i,k){
y = x,x = mul(x,x,n);
if (x == 1 && y != 1 && y != n - 1) return false;
}
if (x != 1) return false;
}
return true;
}
Pollard-Rho大数分解法
我们利用式子\(x^2 + c\)伪随机生成两个数\(a\)和\(b\),判断\(d = (a - b,n)\)是否大于\(1\)小于\(n\),如果是,我们便找打了一个\(n\)的因子\(d\),递归处理\(\frac{n}{d}\)和\(d\)即可,当我们使用以上的素数判定判定出\(n\)是质数时,计入答案
当然我们伪随机生成的两个数可能成环而导致死循环,我们用\(Floyd\)的大步小步法判环即可
具体看代码
LL pr[maxn],pi;
LL gcd(LL a,LL b){return b ? gcd(b,a % b) : a;}
LL Pollard_Rho(LL n){
LL x = random(n),y = x,c = random(n),step = 1,t = 2;
while (true){
step++; x = (mul(x,x,n) + c) % n;
if (y == x) return 1;
LL d = gcd((y - x + n) % n,n);
if (d > 1) return d;
if (step == t) y = x,t <<= 1;
}
}
void Find(LL n){
if (n == 1) return;
if (Miller_Rabin(n)) {pr[++pi] = n; return;}
LL p = n; while (p == n) p = Pollard_Rho(n);
Find(n / p); Find(p);
}
至此我们就可以写出POJ1811
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 100005,maxm = 100005;
const LL INF = 1000000000ll * 1000000000ll;
inline LL read(){
LL out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
inline LL random(LL x){
LL re = 0;
REP(i,4) re = (re << 14) + rand();
return re % x;
}
inline LL mul(LL a,LL b,LL P){
LL re = 0;
for (; b; b >>= 1,a = (a + a) % P)
if (b & 1) re = (re + a) % P;
return re;
}
inline LL qpow(LL a,LL b,LL P){
LL re = 1;
for (; b; b >>= 1,a = mul(a,a,P))
if (b & 1) re = mul(re,a,P);
return re;
}
bool Miller_Rabin(LL n){
if (n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13) return true;
if (n == 1 || n % 2 == 0 || n % 3 == 0 || n % 7 == 0 || n % 11 == 0 || n % 13 == 0) return false;
int T = 50;
LL t = n - 1,k = 0;
while (!(t & 1)) t >>= 1,k++;
while (T--){
LL x = qpow(random(n),t,n),y;
REP(i,k){
y = x,x = mul(x,x,n);
if (x == 1 && y != 1 && y != n - 1) return false;
}
if (x != 1) return false;
}
return true;
}
LL pr[maxn],pi;
LL gcd(LL a,LL b){return b ? gcd(b,a % b) : a;}
LL Pollard_Rho(LL n){
LL x = random(n),y = x,c = random(n),step = 1,t = 2;
while (true){
step++; x = (mul(x,x,n) + c) % n;
if (y == x) return 1;
LL d = gcd((y - x + n) % n,n);
if (d > 1) return d;
if (step == t) y = x,t <<= 1;
}
}
void Find(LL n){
if (n == 1) return;
if (Miller_Rabin(n)) {pr[++pi] = n; return;}
LL p = n; while (p == n) p = Pollard_Rho(n);
Find(n / p); Find(p);
}
int main(){
//srand(998244353);
int T = read(); LL n;
while (T--){
if (Miller_Rabin(n = read())) puts("Prime");
else {
pi = 0; Find(n);
//REP(i,pi) printf("%lld ",pr[i]); puts("");
if (pi == 1) {puts("Prime"); continue;}
LL x = pr[1];
for (int i = 2; i <= pi; i++)
x = min(x,pr[i]);
printf("%lld\n",x);
}
}
return 0;
}
Miiler-Robin素数测试与Pollard-Rho大数分解法的更多相关文章
- Miller-Rabin 素性测试 与 Pollard Rho 大整数分解
\(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...
- 数学--数论--随机算法--Pollard Rho 大数分解算法 (带输出版本)
RhoPollard Rho是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:MillerRabinMillerRabin素数测试. 操作流程 首先,我们先用MillerRabinMille ...
- Miller Rabin素数检测与Pollard Rho算法
一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...
- POJ 2429 GCD & LCM Inverse(Miller-Rabbin素性测试,Pollard rho质因子分解)
x = lcm/gcd,假设答案为a,b,那么a*b = x且gcd(a,b) = 1,因为均值不等式所以当a越接近sqrt(x),a+b越小. x的范围是int64的,所以要用Pollard_rho ...
- 数学--数论--随机算法--Pollard Rho 大数分解算法(纯模板带输出)
ACM常用模板合集 #include <bits/stdc++.h> using namespace std; typedef long long ll; ll pr; ll pmod(l ...
- poj 1811 Prime Test 大数素数测试+大数因子分解
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 27129 Accepted: 6713 Case ...
- poj1881:素因子分解+素数测试
很好的入门题 先测试是否为素数,若不是则进行素因子分解,算法详见总结贴 miller robin 和pollard rho算法 AC代码 #include <iostream> #incl ...
- 初学Pollard Rho算法
前言 \(Pollard\ Rho\)是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:\(MillerRabin\)素数测试(关于\(MillerRabin\),可以参考这篇博客:初学Mi ...
- 整数(质因子)分解(Pollard rho大整数分解)
整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...
随机推荐
- 最近遇到的几个问题,关于Unity脚本在Inspector视图中前面没有勾选框的问题
1.为什么在Inspector视图中有的脚本前面有勾选框,有的脚本前面没有? 当我想要禁用一个脚本时,发现脚本前面没有勾选框,有的却有. 解决办法:在脚本中添加start方法,然后就有勾选框了. 原因 ...
- Unity Shader 学习之旅之SurfaceShader
Unity Shader 学习之旅之SurfaceShader unity shader 图形图像 如果大地的每个角落都充满了光明 谁还需要星星,谁还会 在夜里凝望 寻找遥远的安慰——江河 官方文档 ...
- 程序员应该懂的ip地址知识汇总
1.A类ip由1字节(1字节是8位2进制数)的网络地址和3字节的主机地址组成,网络地址最高位必须是0,地址范围是从1.0.0.0到126.0.0.0,所以A类网络地址有126个,每个网络能容纳至少2^ ...
- mutt命令详解
基础命令学习目录首页 linux命令——mutt的安装和使用[转] 首先介绍一下mutt这个软件,它是一款基于文字界面的邮件客户端,非常小巧,但功能强大,可以用它来读写,回复保存和删除你的邮件,能在l ...
- 三维空间中xoy平面上特定抛物线的正等测投影解析解的一种求法
背景 背景:为锻炼代同学,老师给了她一个反向工程微信"跳一跳"小游戏的任务,希望做一个一样的出来.跳一跳中,有方块,有小人,小人站在方块上. 这个游戏的玩法是,用手指按住手机屏幕, ...
- 学霸网站-Alpha版本发布说明
项目名称 学霸网站 项目版本 Alpha 项目团队 ourteam 发布日期 2014-11-23 一.版本的新功能 1.匿名提问 用户提问的时候可以选择匿名提问,这样在问题的详细信息不会显示提出者的 ...
- 20172329 2018-2019 《Java软件结构与数据结构》实验三报告
20172329 2018-2019-2 <Java软件结构与数据结构>实验三报告 课程:<Java软件结构与数据结构> 班级: 1723 姓名: 王文彬 学号:2017232 ...
- 3、昨天的BUG
基本功能实现了,但是有一些小问题,修改昨天余留的BUG
- cocos2d-x 相关文章资源(安卓开发)
http://blog.csdn.net/sdhjob/article/details/38734993 http://www.cnblogs.com/code4app/p/4026665.html ...
- beta冲刺(7/7)
目录 组员情况 组员1:胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:何宇恒 组员11:刘一好 展示组内最新 ...