数据范围很大,用米勒罗宾测试和Pollard_Rho法可以分解大数。

模板在代码中 O.O

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std; __int64 pri[]= {,,,,,,,,,,};//用小素数表做随机种子避免第一类卡米歇尔数的误判
__int64 multi(__int64 a,__int64 b,__int64 n) //乘法快速幂
{
__int64 tmp=;
while(b)
{
if(b&)
{
tmp+=a;
if(tmp>=n) tmp-=n;
}
a<<=;
if(a>=n) a-=n;
b>>=;
}
return tmp;
}
__int64 multimod(__int64 a,__int64 m,__int64 n) //乘法快速幂
{
__int64 tmp=;
a%=n;
while(m)
{
if(m&) tmp=multi(tmp,a,n);
a=multi(a,a,n);
m>>=;
}
return tmp;
}
__int64 gcd(__int64 a, __int64 b) //迭代算法
{
while(b)
{
__int64 c=a%b;
a=b;
b=c;
}
return a;
}
bool Miller_Rabin(__int64 n) //大素数判断
{
if(n<)
return false;
if(n==)
return true;
if(!(n&))
return false;
__int64 k=,j,m,a;
m=n-;
while(!(m&))
{
m>>=;
k++;
}
for(int i=; i<; i++)
{
if(pri[i]>=n)
return true;
a=multimod(pri[i],m,n);
if(a==)
continue;
for(j=; j<k; j++)
{
if(a==n-)
break;
a=multi(a,a,n);
}
if(j==k)
return false;
}
return true;
}
__int64 pollard_rho(__int64 c,__int64 n) //查找因数
{
__int64 i,x,y,k,d;
i=;
x=y=rand()%n;
k=;
do
{
i++;
d=gcd(n+y-x,n);
if(d> && d<n)
return d;
if(i==k)
{
y=x;
k<<=;
}
x=(multi(x,x,n)+n-c)%n;
}
while(y!=x);
return n;
}
__int64 rho(__int64 n)
{
if(Miller_Rabin(n))
return n;
__int64 t=n;
while(t>=n)
t=pollard_rho(rand()%(n-)+,n);
__int64 a=rho(t);
__int64 b=rho(n/t);
return a<b? a:b;
} __int64 ans[],flag;
void rhoAll(__int64 n) //计算全部质因子
{
if(Miller_Rabin(n))
{
ans[flag++]=n;
return;
}
__int64 t=n;
while(t>=n)
t=pollard_rho(rand()%(n-)+,n);
rhoAll(t);
rhoAll(n/t);
return;
}
int main()
{
//freopen("in.txt","r",stdin);
int t;
__int64 n;
scanf("%d",&t);
while(t--)
{
flag=;
scanf("%I64d",&n);
if(Miller_Rabin(n))
printf("Prime\n");
else
{
//rhoAll(n);
printf("%I64d\n",rho(n));
}
/*for(int i=0;i<flag;i++) //输出全部质因子
if(i!=flag-1)
printf("%I64d ",ans[i]);
else
printf("%I64d\n",ans[i]);*/
}
return ;
}

POJ 1811 大素数判断的更多相关文章

  1. 【转】大素数判断和素因子分解【miller-rabin和Pollard_rho算法】

    集训队有人提到这个算法,就学习一下,如果用到可以直接贴模板,例题:POJ 1811 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/19/2646 ...

  2. POJ 1811 大整数素数判断 Miller_Rabin

    #include <cstdio> #include <cstring> #include <cmath> #include <ctime> #incl ...

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

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

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

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...

  5. HDU 4910 Problem about GCD 找规律+大素数判断+分解因子

    Problem about GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. poj 1811 随机素数和大数分解(模板)

    Sample Input 2 5 10 Sample Output Prime 2 模板学习: 判断是否是素数,数据很大,所以用miller,不是的话再用pollard rho分解 miller : ...

  7. 大素数判断(miller-Rabin测试)

    题目:PolandBall and Hypothesis A. PolandBall and Hypothesis time limit per test 2 seconds memory limit ...

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

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

  9. GCDLCM 【米勒_拉宾素数检验 (判断大素数)】

    GCDLCM 题目链接(点击) 题目描述 In FZU ACM team, BroterJ and Silchen are good friends, and they often play some ...

随机推荐

  1. paip.解决access出现 -2147467259 无效的参数量

    paip.解决access出现 -2147467259   无效的参数量 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http: ...

  2. Linux下程序包管理工具RPM

    实验环境: CentOS release 6.6 (Final)  一台 IP地址:172.16.249.230 RPM 是 Red Hat Package Manager 的缩写,本意是Red Ha ...

  3. 深入理解PHP内核(九)变量及数据类型-静态变量

    原文链接:http://www.orlion.ga/251/ 通常静态变量是静态分配的,他们的生命周期和程序的生命周期一样长,只有在程序退出后才结束生命周期,这和局部变量相反,有的语言中全局变量也是静 ...

  4. Express框架使用以及数据库公共操作类整理(Win7下的NodeJs)

    具体步骤: 1.安装开发工具WebStorm: 2.安装node/npm(下载地址:https://nodejs.org/download/)选择适合你的xxx.mis安装: 3.安装express框 ...

  5. pomelo获取客户端IP

    代码: Handler.prototype.getClientIp = function(msg, session, next) { var ip = session.__session__.__so ...

  6. shell脚本学习心得

    近来主要捣鼓ubuntu,大多数项目中都用到了sh脚本作为启动脚本等,以前只是大概明白如何使用,今天需要自己修改并运行脚本就碰到了很多问题,所以决定静下心来学习一下shell脚本,学习了几个小时,现将 ...

  7. 【大数据】Summingbird(Storm + Hadoop)的demo运行

    一.前言 为了运行summingbird demo,笔者走了很多的弯路,并且在国内基本上是查阅不到任何的资料,耗时很久才搞定了demo的运行.真的是一把辛酸泪,有兴趣想要研究summingbird的园 ...

  8. Java中不同的并发实现的性能比较

    Fork/Join框架在不同配置下的表现如何? 正如即将上映的星球大战那样,Java 8的并行流也是毁誉参半.并行流(Parallel Stream)的语法糖就像预告片里的新型光剑一样令人兴奋不已.现 ...

  9. Azure ARM (6) ARM Template简单介绍

    <Windows Azure Platform 系列文章目录>      Azure ARM (1) 概览      Azure ARM (2) 概览      Azure ARM (3) ...

  10. java并发编程实践学习(2)--对象的组合

    先验条件(Precondition):某些方法包含基于状态的先验条件.例如,不能从空队列中移除一个元素,在删除元素前队列必须处于非空状态.基于状态的先验条件的操作成为依赖状态操作. 在单线程中,如果某 ...