POJ 1811 大素数判断
数据范围很大,用米勒罗宾测试和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 大素数判断的更多相关文章
- 【转】大素数判断和素因子分解【miller-rabin和Pollard_rho算法】
集训队有人提到这个算法,就学习一下,如果用到可以直接贴模板,例题:POJ 1811 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/19/2646 ...
- POJ 1811 大整数素数判断 Miller_Rabin
#include <cstdio> #include <cstring> #include <cmath> #include <ctime> #incl ...
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法) 玄学快
大数因数分解Pollard_rho 算法 复杂度o^(1/4) #include <iostream> #include <cstdio> #include <algor ...
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...
- HDU 4910 Problem about GCD 找规律+大素数判断+分解因子
Problem about GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- poj 1811 随机素数和大数分解(模板)
Sample Input 2 5 10 Sample Output Prime 2 模板学习: 判断是否是素数,数据很大,所以用miller,不是的话再用pollard rho分解 miller : ...
- 大素数判断(miller-Rabin测试)
题目:PolandBall and Hypothesis A. PolandBall and Hypothesis time limit per test 2 seconds memory limit ...
- 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429
素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...
- GCDLCM 【米勒_拉宾素数检验 (判断大素数)】
GCDLCM 题目链接(点击) 题目描述 In FZU ACM team, BroterJ and Silchen are good friends, and they often play some ...
随机推荐
- Android上dip、dp、px、sp等单位说明
Android上dip.dp.px.sp等单位说明 dip device independent pixels(设备独立像素). 不同设备不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA ...
- salesforce 零基础学习(三十一)关于LookUp字段点击Save时的Validation
今天在群里大概遇到了这样一个问题,明明这个User存在,但是save的时候提示信息说Value不存在,大概的意思如下图所示,目前认为这种问题常见的可能情况有两种. 一.此字段设置Validation ...
- Android笔记——Android框架
本篇将站在顶级的高度--架构,来看android.我开篇就说了,这个系列适合0基础的人且我也是从0开始按照这个步骤来 学的,谈架构是不是有点螳臂挡车,自不量力呢?我觉得其实不然,如果一开始就对整个an ...
- react6 事件传递参数
<body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...
- JsBridge实现Javascript和Java的互相调用
前端网页Javascript和Native互相调用在手机应用中越来越常见,JsBridge是最常用的解决方案. 在Android开发中,能实现Javascript与Native代码通信的,有4种途径: ...
- 在SSIS中的不同组件间使用局部临时表
Connetion的属性RetainSameConnection是个boolean值,指定是否保持相同的链接,默认值是false,表示每个component都会单独的使用connection,在com ...
- 强制SQL Server执行计划使用并行提升在复杂查询语句下的性能
最近在给一个客户做调优的时候发现一个很有意思的现象,对于一个复杂查询(涉及12个表)建立必要的索引后,语句使用的IO急剧下降,但执行时间不降反升,由原来的8秒升到20秒. 通过观察执行 ...
- JSP网站开发基础总结《十四》
对于JSP的知识总结,已经为大家分享的差不多了,本篇就为大家做一下最后总结,本篇总结的主题是文件上传和下载,在之前的随笔中也有为大家分享过文件上传和下载的知识,不过都是通过JAVA提供的上传下载类实现 ...
- 《BI那点儿事》Microsoft 时序算法——验证神奇的斐波那契数列
斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10 ...
- Linux rsync实现断点续传
Linux 主机之间即时传送文件,scp命令大家都很熟悉但当要传送的文件较大,过程中如果网络中断了,就比较悲剧了.这时候可以考虑使用rsync命令替代scp,实现断点续传文件. 试验:rsync使用 ...