论O(1)快速乘和O(logn)快速乘的差距….

//By SiriusRen
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
ll shai[10]={2,3,5,7,11,13,17,19,23,29};
ll mul(ll a,ll b,ll p){
ll d=((long double)a/p*b+1e-8);
ll res=a*b-d*p;
res=res<0?res+p:res;
return res;
}
ll pow(ll x,ll y,ll mod){
x%=mod;ll res=1;
while(y){
if(y&1)res=mul(res,x,mod);
x=mul(x,x,mod),y>>=1;
}return res;
}
bool check(ll a,ll n,ll r,int s){
ll x=pow(a,r,n),pre=x;
for(int i=1;i<=s;i++){
x=mul(x,x,n);
if(x==1&&pre!=1&&pre!=n-1)return 0;
pre=x;
}return x==1;
}
bool miller_rabin(ll n){
if(n<=1)return 0;
ll r=n-1,s=0;
while(!(r&1))r>>=1,s++;
for(int i=0;i<10;i++){
if(shai[i]==n)return 1;
if(!check(shai[i],n,r,s))return 0;
}return 1;
}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
ll prime_factor(ll n,ll c){
ll k=2,x=rand()%n,y=x,p=1;
for(int i=1;p==1;i++){
x=(mul(x,x,n)+c)%n;
p=gcd(abs(x-y),n);
if(i==k)y=x,k<<=1;
}return p;
}
ll ans,xx;int cases;
void pollard_rho(ll n){
if(n==1)return;
if(miller_rabin(n)){ans=max(ans,n);return;}
ll p=n;
while(p==n)p=prime_factor(n,rand()%(n-1));
pollard_rho(p),pollard_rho(n/p);
}
int main(){
scanf("%d",&cases);
while(cases--){
ans=0,scanf("%lld",&xx),pollard_rho(xx);
if(ans!=xx)printf("%lld\n",ans);
else puts("Prime");
}
}

//By SiriusRen
#include <cstdio>
#include <algorithm>
using namespace std;
typedef unsigned long long ll;
ll shai[10]={2,3,5,7,11,13,17,19,23,29};
ll mul(ll x,ll y,ll mod){
x%=mod;ll res=0;
while(y){
if(y&1)res=(res+x)%mod;
x=(x+x)%mod;
y>>=1;
}return res;
}
ll pow(ll x,ll y,ll mod){
x%=mod;ll res=1;
while(y){
if(y&1)res=mul(res,x,mod);
x=mul(x,x,mod),y>>=1;
}return res;
}
bool check(ll a,ll n,ll r,int s){
ll x=pow(a,r,n),pre=x;
for(int i=1;i<=s;i++){
x=mul(x,x,n);
if(x==1&&pre!=1&&pre!=n-1)return 0;
pre=x;
}return x==1;
}
bool miller_rabin(ll n){
if(n<=1)return 0;
ll r=n-1,s=0;
while(!(r&1))r>>=1,s++;
for(int i=0;i<10;i++){
if(shai[i]==n)return 1;
if(!check(shai[i],n,r,s))return 0;
}return 1;
}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
ll prime_factor(ll n,ll c){
ll k=2,x=rand()%n,y=x,p=1;
for(int i=1;p==1;i++){
x=(mul(x,x,n)+c)%n;
p=gcd(abs((long long)x-(long long)y),(long long)n);
if(i==k)y=x,k<<=1;
}return p;
}
ll ans,xx;int cases;
void pollard_rho(ll n){
if(n==1)return;
if(miller_rabin(n)){ans=max(ans,n);return;}
ll p=n;
while(p==n)p=prime_factor(n,rand()%(n-1));
pollard_rho(p),pollard_rho(n/p);
}
int main(){
scanf("%d",&cases);
while(cases--){
ans=0,scanf("%llu",&xx),pollard_rho(xx);
if(ans!=xx)printf("%llu\n",ans);
else puts("Prime");
}
}

BZOJ 3667 Pollard-rho &Miller-Rabin的更多相关文章

  1. poj 1811 Pallor Rho +Miller Rabin

    /* 题目:给出一个数 如果是prime 输出prime 否则输出他的最小质因子 Miller Rabin +Poller Rho 大素数判定+大数找质因子 后面这个算法嘛 基于Birthday Pa ...

  2. Pollard Rho算法浅谈

    Pollard Rho介绍 Pollard Rho算法是Pollard[1]在1975年[2]发明的一种将大整数因数分解的算法 其中Pollard来源于发明者Pollard的姓,Rho则来自内部伪随机 ...

  3. Pollard rho算法+Miller Rabin算法 BZOJ 3668 Rabin-Miller算法

    BZOJ 3667: Rabin-Miller算法 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 1044  Solved: 322[Submit][ ...

  4. POJ2429 - GCD & LCM Inverse(Miller–Rabin+Pollard's rho)

    题目大意 给定两个数a,b的GCD和LCM,要求你求出a+b最小的a,b 题解 GCD(a,b)=G GCD(a/G,b/G)=1 LCM(a/G,b/G)=a/G*b/G=a*b/G^2=L/G 这 ...

  5. POJ1811- Prime Test(Miller–Rabin+Pollard's rho)

    题目大意 给你一个非常大的整数,判断它是不是素数,如果不是则输出它的最小的因子 题解 看了一整天<初等数论及其应用>相关部分,终于把Miller–Rabin和Pollard's rho这两 ...

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

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

  7. HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...

  8. POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

  9. POJ2429_GCD &amp; LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】

    GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 ...

  10. POJ1811_Prime Test【Miller Rabin素数測试】【Pollar Rho整数分解】

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

随机推荐

  1. javascript一个作用域案例分析

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. poj3083 Children of the Candy Corn 深搜+广搜

    这道题有深搜和广搜.深搜还有要求,靠左或靠右.下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向.向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标).其他方向也可以这样确定.通过上一步 ...

  3. 最简单的一致性Hash算法实现

    import java.util.Collection;import java.util.SortedMap;import java.util.TreeMap; public class Consis ...

  4. WAMP安装之坑

    Apache安装目录不能有空格 Apache根目录修改后不能直接localhost打开,可以通过改变端口,然后输入 localhost:端口号 打开

  5. 04--深入探讨C++中的引用

    深入探讨C++中的引用           引用是C++引入的新语言特性,是C++常用的一个重要内容之一,正确.灵活地使用引用,可以使程序简洁.高效.我在工作中发现,许多人使用它仅仅是想当然,在某些微 ...

  6. MVC 入口

    1.在 Global.asax public class MvcApplication : System.Web.HttpApplication { protected void Applicatio ...

  7. JavaScript进阶【五】利用JavaScript实现动画的基本思路

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. jquery ajax 全介绍

    下面是Jquery中AJAX参数详细列表: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type String (默认: "GET") 请求 ...

  9. el表达式中的比较和包含

    相等( equal ) :eq 不相等( not equal ): ne / neq 大于( greater than ): gt 小于( less than ): lt 大于等于( great th ...

  10. ioremap映射函数

    一.ioremap() 函数 Linux在io.h头文件中声明了函数ioremap(),用来将I/O内存资源的物理地址映射到核心虚地址空间(3GB-4GB)中(这里是内核空间),原型如下: 1.ior ...