板题

Miiler-Robin素数测试

目前已知分解质因数以及检测质数确定性方法就只能\(sqrt{n}\)试除

但是我们可以基于大量测试的随机算法而有大把握说明一个数是质数

Miler-Robin素数测试基于以下两个原理:

费马小定理

即我们耳熟能详的

对于质数\(p\)

\[a^{p - 1} \equiv 1 \pmod p
\]

二次探测原理

对于质数\(p\),如果存在\(x\)满足

\[x^2 \equiv 1 \pmod p
\]

那么\(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大数分解法的更多相关文章

  1. Miller-Rabin 素性测试 与 Pollard Rho 大整数分解

    \(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...

  2. 数学--数论--随机算法--Pollard Rho 大数分解算法 (带输出版本)

    RhoPollard Rho是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:MillerRabinMillerRabin素数测试. 操作流程 首先,我们先用MillerRabinMille ...

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

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

  4. 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 ...

  5. 数学--数论--随机算法--Pollard Rho 大数分解算法(纯模板带输出)

    ACM常用模板合集 #include <bits/stdc++.h> using namespace std; typedef long long ll; ll pr; ll pmod(l ...

  6. poj 1811 Prime Test 大数素数测试+大数因子分解

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 27129   Accepted: 6713 Case ...

  7. poj1881:素因子分解+素数测试

    很好的入门题 先测试是否为素数,若不是则进行素因子分解,算法详见总结贴 miller robin 和pollard rho算法 AC代码 #include <iostream> #incl ...

  8. 初学Pollard Rho算法

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

  9. 整数(质因子)分解(Pollard rho大整数分解)

    整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...

随机推荐

  1. [Codeforces-911B] - Two Cakes

    B. Two Cakestime limit per test 1 secondmemory limit per test 256 megabytesinput standard inputoutpu ...

  2. Netty源码分析第7章(编码器和写数据)---->第1节: writeAndFlush的事件传播

    Netty源码分析第七章: 编码器和写数据 概述: 上一小章我们介绍了解码器, 这一章我们介绍编码器 其实编码器和解码器比较类似, 编码器也是一个handler, 并且属于outbounfHandle ...

  3. awk之close函数

    echo "21 2 " | awk '{ first[NR]=$ second[NR]=$ }END{ print "======打印第1列并排序:========== ...

  4. teamwork 2

    1.访问上学期项目团队,学习他们的得失. 上学期学长们有一个项目是学霸系统,在看过了学长们的相关博客后,我们可以感受到学长们确实花费了不少心思,也看到了许多值得我们学习的地方. 首先,学长们在项目开始 ...

  5. 实验1:java开发环境的熟悉

    一.实验内容 1. 使用JDK编译.运行简单的Java程序 2.使用Eclipse 编辑.编译.运行.调试Java程序 3.实现四则运算并进行测试. 二.实验知识点 1. JVM.JRE.JDK的安装 ...

  6. Think In Java读书笔记:内部类覆盖及其初始化

    本文相关章节:第十章 内部类 10.10 内部类可以被覆盖吗 在读至本节第二个范例代码时(及下方的代码),我对输出结果中的第一个“Egg.Yolk()”很不理解,为什么它会第一个地方输出. 我起初认为 ...

  7. 结对项目:四则运算web

    1)Coding.Net项目地址 https://git.coding.net/DandelionClaw/WEB_Calculator.git 注:本项目为web端,并且需要连接SQL Server ...

  8. 利用CNN进行多分类的文档分类

    # coding: utf-8 import tensorflow as tf class TCNNConfig(object): """CNN配置参数"&qu ...

  9. nigix安装

    树莓派安装nginx,参考http://blog.csdn.net/zizi7/article/details/54347223 1. 下载PCRE 并安装. 主页地址: ftp://ftp.csx. ...

  10. Python开发【第五篇】迭代器、生成器、递归函数、二分法

    阅读目录 一.迭代器 1. 迭代的概念 #迭代器即迭代的工具(自定义的函数),那什么是迭代呢? #迭代:指一个重复的过程,每次重复都可以称之为一次迭代,并且每一次重复的结果是下一个迭代的初始值(例如: ...