题目链接

Pollard_Rho:http://blog.csdn.net/thy_asdf/article/details/51347390

#include<cstdio>
#include<cctype>
#include<algorithm>
#define gc() getchar()
const int p[]={2,3,5,7,11,13,17,19};
typedef long long LL;
LL Ans; inline LL read()
{
LL now=0,f=1;register char c=gc();
for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
}
inline LL Mult(LL a,LL b,LL p)//O(1)快速乘
{
LL tmp=a*b-(LL)((long double)a/p*b+1e-8)*p;
return tmp<0?tmp+p:tmp;
}
LL Fast_Pow(LL n,LL k,LL p)
{
LL t=1;
for(;k;k>>=1,n=n*n%p)
if(k&1) t=t*n%p;
return t;
}
bool Miller_Rabin(LL n)
{
if(n==2) return 1;
if(!(n&1)||n==1) return 0;
for(int i=0;i<8;++i)
if(n==p[i]) return 1;
else if(!(n%p[i])) return 0;
LL u=n-1,now,las; int t=0;
while(!(u&1)) u>>=1,++t;
for(int i=0;i<8;++i)
{
now=Fast_Pow(p[i],u,n);
for(int j=1;j<=t;++j)
{
las=now, now=Mult(now,now,n);
if(now==1&&las!=1&&las!=n-1) return 0;
}
if(now!=1) return 0;
}
return 1;
}
LL gcd(LL x,LL y)
{
return y?gcd(y,x%y):x;
}
LL Rho(LL n,LL delta)
{//现要分解n,有两个随机数x,y,若p=gcd(x-y,n)!=1&&p!=n,那么p为n的一个约数...省略
LL x=rand()%n,y=x,p=1; int k=2;//设定k为此次路径长
for(int i=1;p==1;++i)
{
x=(Mult(x,x,n)+delta)%n;//随机函数f(x)=x*x+d
p=gcd(std::abs(x-y),n);//多次生成随机数,直至找到p是n的一个因子
if(i==k) y=x,k<<=1;//达到k次后把y赋值为x。路径每次倍长
}
return p;
}
void Find(LL n)
{
if(n==1) return;
if(Miller_Rabin(n)) {Ans=std::max(Ans,n);/*fac[++cnt]=n;*/ return;}
LL t=n;
while(t==n) t=Rho(n,rand()%(n-1)+1);
//t=n说明这个随机函数会导致走到n的环上,再换一个重试即可
Find(t), Find(n/t);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("3667.in","r",stdin);
#endif int t=read();LL n;
while(t--)
n=read(),Ans=0,Find(n),Ans==n?puts("Prime"):printf("%lld\n",Ans);
return 0;
}

BZOJ.3667.Rabin-Miller算法(MillerRabin PollardRho)的更多相关文章

  1. [Miller-Rabin & Pollard-rho]【学习笔记】

    Miller-Rabin & Pollard-rho 很久之前就学过了...今天重学一遍 利用费马小定理,但不能判断伪素数的情况 基于a的伪素数n: \(a^{n-1} \equiv 1 \p ...

  2. 【模板】SPOJ FACT0 大数分解 miller-rabin & pollard-rho

    http://www.spoj.com/problems/FACT0/en/ 给一个小于1e15的数,将他分解. miller-rabin & pollard-rho模板 #include & ...

  3. bzoj 3667: Rabin-Miller算法【Miller-Rabin】

    Miller-Rabin模板 #include<iostream> #include<cstdio> #include<algorithm> using names ...

  4. BZOJ 3667 Pollard-rho &Miller-Rabin

    论O(1)快速乘和O(logn)快速乘的差距-. //By SiriusRen #include <cstdio> #include <algorithm> using nam ...

  5. BZOJ 3667: Rabin-Miller算法 (Pollard-Rho 模板)

    说实话,我知道每一步都干啥,但我完全不知道为啥这么做,也不知道为什么是正确的,反正会用就行了~ #include <cmath> #include <cstdio> #incl ...

  6. 【刷题】BZOJ 3667 Rabin-Miller算法

    Input 第一行:CAS,代表数据组数(不大于350),以下CAS行,每行一个数字,保证在64位长整形范围内,并且没有负数.你需要对于每个数字:第一,检验是否是质数,是质数就输出Prime 第二,如 ...

  7. bzoj 3667 Rabin-Miller算法

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...

  8. 数论入门2——gcd,lcm,exGCD,欧拉定理,乘法逆元,(ex)CRT,(ex)BSGS,(ex)Lucas,原根,Miller-Rabin,Pollard-Rho

    数论入门2 另一种类型的数论... GCD,LCM 定义\(gcd(a,b)\)为a和b的最大公约数,\(lcm(a,b)\)为a和b的最小公倍数,则有: 将a和b分解质因数为\(a=p1^{a1}p ...

  9. 梅森素数 判定总结 - Lucas-Lehmer算法 & Miller-rabin算法

    梅森素数 定义: if m是一个正整数 and 2^m-1是一个素数 then m是素数 if m是一个正整数 and m是一个素数 then M(m)=2^m-1被称为第m个梅森数 if p是一个素 ...

随机推荐

  1. numpy中 array数组的shape属性

    numpy.array 的shape属性理解 在码最邻近算法(K-Nearest Neighbor)的过程中,发现示例使用了numpy的array数组管理,其中关于array数组的shape(状态)属 ...

  2. u盘的一些理解

    U盘是由主控板+FLASH+外壳组成的,当主控板焊接上空白FLASH后插入电脑,因为没有相应的数据,  量产工具 电脑只能识别到主控板,而无法识别到FLASH,所以这时候电脑上显示出U盘盘符,但是双击 ...

  3. Python select IO多路复用

    一.select介绍 Python的select()函数是底层操作系统实现的直接接口.它监视套接字,打开文件和管道(任何带有返回有效文件描述符的fileno()方法),直到它们变得可读或可写,或者发生 ...

  4. 配置mysql5.5主从复制、半同步复制、主主复制

    mysql主服务器 192.168.8.40 mysql从服务器 192.168.8.41 全新配置过程(主和从数据库都没有数据): 主从复制主服务器设置: 1.改server-id      2.启 ...

  5. 转载:编译安装Nginx(1.5.1)《深入理解Nginx》(陶辉)

    原文:https://book.2cto.com/201304/19618.html 1.5 configure详解 可以看出,configure命令至关重要,下文将详细介绍如何使用configure ...

  6. Linux设备模型:基础篇

    linux提供了新的设备模型:总线(bus).设备(device).驱动(driver).其中总线是处理器与设备之间通道,在设备模型中,所有的设备都通过总线相连:设备是对于一个设备的详细信息描述,驱动 ...

  7. 类(字符串型;日期时间型;Math)

  8. LeetCode(9):回文数

    Easy! 题目描述: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: f ...

  9. HRBUST - 1818 石子合并 区间dp入门

    有点理解了进阶指南上说的”阶段,状态和决策“ /* 区间dp的基础题: 以区间长度[2,n]为阶段,枚举该长度的区间,状态dp[l][r]表示合并区间[l,r]的最小费用 状态转移方程dp[l][r] ...

  10. hdu4190 二分答案

    /*二分答案即可*/ #include<bits/stdc++.h> #define maxn 500005 #define ll long long #define INF 500000 ...