Int64以内Rabin-Miller强伪素数测试和Pollard 因数分解的算法实现

选取随机数\(a\) 随机数\(b\),检查\(gcd(a - b, n)\)是否大于1,若大于1则\(a - b\)是\(n\)的一个因数

实现1:floyd判环

利用多项式\(f(x)\)迭代出\({x_0, x_1, \dots, x_k}\)

设定\(x = y = x_0\)的初始值,选用多项式进行迭代,每次:\(x = f(x)\), \(y = f(f(y))\),即:\(x = x_k, y = x_{2k}\)当\(x == y\)时出现循环

设\(x = y = 2\),\(f(n) = n^2 + a\)

typedef long long ll;

ll mul_mod(ll a, ll b, ll m){
ll ans = 0, exp = a;
while(a >= m) a -= m;
while(b){
if(b & 1){
ans += exp;
while(ans >= m) ans -= m;
}
exp += exp;
while(exp >= m) exp -= m;
b >>= 1;
}
return ans;
} ll pollard_rho(ll n, int a){
ll x = 2, y = 2, d = 1;
while(d == 1){
x = mul_mod(x, x, n) + a;
y = mul_mod(y, y, n) + a;
y = mul_mod(y, y, n) + a;
d = __gcd((x >= y ? x - y : y - x), n);
}
if(d == n) return pollard_rho(n, a + 1);
return d;
}

实现2: brent判环(更高效)

不同于floyd每次计算\(x_k, x_{2k}\)进行判断,brent每次只计算\(x_k\),当k是2的方幂时,\(y = x_k\),每次计算\(d = gcd(x_k - y, n)\)

typedef long long ll;

ll mul_mod(ll a, ll b, ll m){
ll ans = 0, exp = a;
while(a >= m) a -= m;
while(b){
if(b & 1){
ans += exp;
while(ans >= m) ans -= m;
}
exp += exp;
while(exp >= m) exp -= m;
b >>= 1;
}
return ans;
} ll pollard_rho(ll n, int a){
ll x = 2, y = 2, d = 1, k = 0, i = 1;
while(d == 1){
++k;
x = mul_mod(x, x, n) + a;
d = __gcd(x >= y ? x - y : y - x, n);
if(k == i){
y = x;
i <<= 1;
}
}
if(d == n) return pollard_rho(n, a + 1);
return d;
}

Pollard_rho 因数分解的更多相关文章

  1. 数论知识总结——史诗大作(这是一个flag)

    1.快速幂 计算a^b的快速算法,例如,3^5,我们把5写成二进制101,3^5=3^1*1+3^2*2+3^4*1 ll fast(ll a,ll b){ll ans=;,a=mul(a,a)))a ...

  2. 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429

    素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...

  3. 与数论的厮守02:整数的因子分解—Pollard_Rho

    学Pollard_Rho之前,你需要学会:Miller Rabin. 这是一个很高效的玄学算法,用来对大整数进行因数分解. 我们来分解n.若n是一个素数,那么就不需要分解了.所以我们还得能够判断一个数 ...

  4. POJ 1811 Prime Test (Rabin-Miller强伪素数测试 和Pollard-rho 因数分解)

    题目链接 Description Given a big integer number, you are required to find out whether it's a prime numbe ...

  5. 大素数判断和素因子分解(miller-rabin,Pollard_rho算法) 玄学快

    大数因数分解Pollard_rho 算法 复杂度o^(1/4) #include <iostream> #include <cstdio> #include <algor ...

  6. @总结 - 10@ Miller-Rabin素性测试与Pollard-Rho因数分解

    目录 @1 - 素性测试:Miller-Rabin算法@ @1.1 - 算法来源@ @1.2 - 算法描述@ @1.3 - 算法实现@ @2 - 因数分解:Pollard-Rho算法@ @2.0 - ...

  7. 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)

    4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 290  Solved: 148[Submit][Status ...

  8. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  9. Vijos1889 天真的因数分解

    描述 小岛: 什么叫做因数分解呢?doc : 就是将给定的正整数n, 分解为若干个素数连乘的形式.小岛: 那比如说 n=12 呢?doc : 那么就是 12 = 2 X 2 X 3 呀.小岛: 呜呜, ...

随机推荐

  1. yum无法正常安装,提示如下 There are no enabled repos Run "yum repolist all"

    一般来说著名的linux系统基本上分两大类:1 RedHat系列:Redhat.Centos.Fedora等2 Debian系列:Debian.Ubuntu等RedHat 系列:1 常见的安装包格式 ...

  2. apache配置apache server status,监控服务器访问情况

    在apache配置文件中添加开启代码, 1.如果你的Apache配置文件httpd.conf或者extra/httpd-info.conf中有LoadModule status_module modu ...

  3. linux系统更改当前主机名

    问题描述:CentOS系统,默认的主机名为localhost.localdomain,刚开始安装的时候会提示修改,但是有时候会忽略,那安装好后怎么修改呢? 解决方法: 1.以根用户登录,输入hostn ...

  4. jQuery实现单击和鼠标感应事件。

    1.实现单击事件动态交替http://www.cnblogs.com/ahthw/p/4232837.html讲到了toggleClass(),对于单击事件而言,jQuery同样提供了动态交替的tog ...

  5. explode 把字符串打散为数组

    // 显示的字段列表 $smarty->assign('field_show', explode(',',$list_name)); explode(separator,string,limit ...

  6. @RequestBody与serialize()、serializeArray()、拼接Json 妙用总结

    @requestBody注解常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容, 比如说:application/json或者是app ...

  7. mysql-profiling详解

    要想优化一条 Query,我们就需要清楚的知道这条 Query 的性能瓶颈到底在哪里,是消耗的 CPU计算太多,还是需要的的 IO 操作太多?要想能够清楚的了解这些信息,在 MySQL 5.0 和 M ...

  8. shell脚本报错 value too great for base

    此错误是shell脚本在计算以0开头的数字时,默认以8进制进行计算,导致在计算08时超过了8进制的范围,报此错误. shell脚本代码如下: #!/bin/bash a= ..} do a=$[$a+ ...

  9. 一台电脑启动多个tomcat

    原文 http://dong-shuai22-126-com.iteye.com/blog/1763666 如果现在一台机器上已经部署了一个tomcat服务,无论这个tomcat是否已经注册为服务了, ...

  10. laravel JWT Auth - JSON Web令牌认证API

    https://github.com/tymondesigns/jwt-auth/wiki