BZOJ 3667: Rabin-Miller算法 (Pollard-Rho 模板)
说实话,我知道每一步都干啥,但我完全不知道为啥这么做,也不知道为什么是正确的,反正会用就行了~
#include <cmath>
#include <cstdio>
#include <algorithm>
#define ll long long
#define ull unsigned long long
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
int array[20]={2,3,5,7,11,13,17,19};
ll Max;
ll mult(ll x,ll y,ll mod)
{
ll tmp=(long double)x/mod*y;
return ((ull)x*y-(ull)tmp*mod+mod)%mod;
}
ll qpow(ll base,ll k,ll mod)
{
ll tmp=1;
for(;k;k>>=1,base=mult(base,base,mod)) if(k&1) tmp=mult(tmp,base,mod);
return tmp;
}
ll F(ll x,ll c,ll mod)
{
return (mult(x,x,mod)+c)%mod;
}
int isprime(ll x)
{
if(x<=1) return 1;
int k,i,j;
ll cur,a,pre;
for(k=0,cur=x-1;cur%2==0;cur>>=1) ++k;
for(i=0;i<8;++i)
{
if(x==array[i]) return 1;
pre=a=qpow(array[i],cur,x);
for(j=1;j<=k;++j)
{
a=mult(a,a,x);
if(a==1&&pre!=1&&pre!=x-1) return 0;
pre=a;
}
if(a!=1) return 0;
}
return 1;
}
ll pollard_rho(ll x)
{
ll s=0,t=0,c=rand()%(x-1)+1,val=1,d;
int k,step;
for(k=1;;k<<=1,s=t,val=1)
{
for(step=1;step<=k;++step)
{
t=F(t,c,x);
val=mult(val,abs(s-t),x);
if(step%127==0)
{
d=__gcd(val,x);
if(d>1) return d;
}
}
d=__gcd(val,x);
if(d>1) return d;
}
}
void solve(ll x)
{
if(x<=Max||x<2) return;
if(isprime(x))
{
Max=max(Max,x);
return;
}
ll p=x;
for(;p>=x;) p=pollard_rho(x);
for(;x%p==0;) x/=p;
solve(x),solve(p);
}
int main()
{
int T,i,j;
// setIO("input");
scanf("%d",&T);
for(i=1;i<=T;++i)
{
ll a;
scanf("%lld",&a),Max=0,solve(a);
if(Max==a) printf("Prime\n");
else printf("%lld\n",Max);
}
return 0;
}
BZOJ 3667: Rabin-Miller算法 (Pollard-Rho 模板)的更多相关文章
- HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解
链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...
- 数学--数论--随机算法--Pollard Rho 大数分解算法(纯模板带输出)
ACM常用模板合集 #include <bits/stdc++.h> using namespace std; typedef long long ll; ll pr; ll pmod(l ...
- 数学--数论--随机算法--Pollard Rho 大数分解算法 (带输出版本)
RhoPollard Rho是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:MillerRabinMillerRabin素数测试. 操作流程 首先,我们先用MillerRabinMille ...
- Pollard rho模板
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #in ...
- Pollard rho算法+Miller Rabin算法 BZOJ 3668 Rabin-Miller算法
BZOJ 3667: Rabin-Miller算法 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 1044 Solved: 322[Submit][ ...
- Miller Rabin素数检测与Pollard Rho算法
一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...
- 【刷题】BZOJ 3667 Rabin-Miller算法
Input 第一行:CAS,代表数据组数(不大于350),以下CAS行,每行一个数字,保证在64位长整形范围内,并且没有负数.你需要对于每个数字:第一,检验是否是质数,是质数就输出Prime 第二,如 ...
- Pollard Rho算法浅谈
Pollard Rho介绍 Pollard Rho算法是Pollard[1]在1975年[2]发明的一种将大整数因数分解的算法 其中Pollard来源于发明者Pollard的姓,Rho则来自内部伪随机 ...
- Pollard Rho 算法简介
\(\text{update 2019.8.18}\) 由于本人将大部分精力花在了cnblogs上,而不是洛谷博客,评论区提出的一些问题直到今天才解决. 下面给出的Pollard Rho函数已给出散点 ...
- 初学Pollard Rho算法
前言 \(Pollard\ Rho\)是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:\(MillerRabin\)素数测试(关于\(MillerRabin\),可以参考这篇博客:初学Mi ...
随机推荐
- 用SPFA 解决POJ2240
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30790 Accepted: 12761 Descr ...
- C#面向对象17 23种设计模式
1.简单工厂模式 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- golang(8):channel读写 & goroutine 通信
goroutine 1.进程和线程 A. 进程是程序在操作系统中的一次执行过程,系统进行资源分配和调度的一个独立单位 B. 线程是进程的一个执行实体,是CPU调度和分派的基本单位,它是比进程更小的能独 ...
- JS基础_Unicode编码表
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- O052、Create Volume 操作 (Part III)
参考https://www.cnblogs.com/CloudMan6/p/5617980.html Jun 20 17:15:56 DevStack-Rocky-Compute-22 c ...
- 剖析Vue之双向数据绑定
vue.js 是采用数据劫持结合发布者-订阅者模式的方式,通过Object.defineProperty()来劫持各个属性的setter,getter,在数据变动时发布消息给订阅者,触发相应的监听回调 ...
- vue-cli 3.0不能用module.exports为undefined,导致第三方js都没有办法引用
新的 vue-cli 默认禁止了 commonjs 语法可以添加 babel 插件解决 yarn add @babel/plugin-transform-modules-commonjs -D 或 n ...
- 完美解决Uncaught SyntaxError: Unexpected end of input
Unexpected end of input 的英文意思是“意外的终止输入” 他通常表示我们浏览器在读取我们的js代码时,碰到了不可预知的错误,导致浏览器 无语进行下面的读取 通常造成这种错误的原 ...
- 【转载】linux SUID SGID
作者:sparkdev 出处:http://www.cnblogs.com/sparkdev/ setuid 和 setgid 分别是 set uid ID upon execution 和 set ...
- Redis-Hash常用命令
Redis-Hash常用命令 hset key field value 设置一个散列,但是在散列中一次只能设置一个属性,如果要批量设置多个属性,则需要使用 hmset命令 hget key field ...