poj 1811 Prim test
基本上一个裸的Miller_Rabin大素数判定和一个裸的Pollard_rho素数分解算法,当模板用吧!
#include<cstdio>
#include<algorithm>
#include<stdlib.h>
#define LL long long
using namespace std;
const int s=;
int tol;
LL factor[]; LL mult_mod(LL a,LL b,LL c)//计算a*b%c;
{
LL ret=;
a%=c;
b%=c;
while(b>)
{
if(b&) ret=(ret+a)%c;
a<<=;
if(a>=c) a%=c;
b>>=;
}
return ret;
} LL pow_mod(LL a,LL b,LL mod)//计算a^b%b
{
if(b==) return a%mod;
a%=mod;
LL tmp=a;
LL ret=;
while(b>)
{
if(b&) ret=mult_mod(ret,tmp,mod);
tmp=mult_mod(tmp,tmp,mod);
b>>=;
}
return ret;
} //以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
//一定是合数返回true,不一定返回false
bool check(LL a,LL n,LL x,LL t)
{
LL ret=pow_mod(a,x,n);
LL last=ret;
for(int i=; i<=t; i++)
{
ret=mult_mod(ret,ret,n);
if(ret==&&last!=&&last!=n-) return ;
last=ret;
}
if(ret!=) return ;
return false;
} // Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false;
bool Miller_Rabin(LL a)
{
if(a<) return ;
if(a==) return ;
if((a&==)) return ;
LL x=a-;
LL t=;
while((x&)==)
{
x>>=;
t++;
}
for(int i=; i<s; i++)
{
long long b=rand()%(a-)+;
if(check(b,a,x,t))
return ;
}
return ;
} LL gcd(LL a,LL b)
{
if(a==)return ;
if(a<) return gcd(-a,b);
while(b)
{
LL t=a%b;
a=b;
b=t;
}
return a;
} LL Pollard_rho(LL x,LL c)
{
LL i=,k=;
LL x0=rand()%x;
LL y=x0;
while()
{
i++;
x0=(mult_mod(x0,x0,x)+c)%x;
LL d=gcd(y-x0,x);
if(d!=&&d!=x) return d;
if(y==x0) return x;
if(i==k)
{
y=x0;
k+=k;
}
}
} void findfac(LL n)
{
if(Miller_Rabin(n))//素数
{
factor[tol++]=n;
return;
}
LL p=n;
while(p>=n)p=Pollard_rho(p,rand()%(n-)+);
findfac(p);
findfac(n/p);
} int main()
{
LL a;
int n;
scanf("%d",&n);
while(n--)
{
scanf("%lld",&a);
if(Miller_Rabin(a))
{
puts("Prime");
continue;
}
tol=;
findfac(a);
LL ans=factor[];
for(int i=;i<tol;i++)
if(factor[i]<ans)
ans=factor[i];
printf("%lld\n",ans);
}
return ;
}
poj 1811 Prim test的更多相关文章
- 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429
素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...
- POJ 1811 Prime Test (Rabin-Miller强伪素数测试 和Pollard-rho 因数分解)
题目链接 Description Given a big integer number, you are required to find out whether it's a prime numbe ...
- Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test
POJ 1811 Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 32534 Accepted: 8 ...
- POJ 2421(prim)
http://poj.org/problem?id=2421 这个题和poj1258是一样的,只要在1258的基础上那么几行代码,就可以A,水. 题意:还是n连通问题,和1258不同的就是这个还有几条 ...
- Poj(1251),Prim字符的最小生成树
题目链接:http://poj.org/problem?id=1251 字符用%s好了,方便一点. #include <stdio.h> #include <string.h> ...
- POJ 2031 prim
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4400 Accepted: 2 ...
- 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29046 Accepted: 7342 Case ...
- poj 1811 大数分解
模板 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> ...
- poj 1811 Pallor Rho +Miller Rabin
/* 题目:给出一个数 如果是prime 输出prime 否则输出他的最小质因子 Miller Rabin +Poller Rho 大素数判定+大数找质因子 后面这个算法嘛 基于Birthday Pa ...
随机推荐
- MATLAB 误差函数erf(x)
误差函数: 1.误差函数定义为: 它的性质如下: 2 互补误差函数定义为: 它具有如下性质: 下表给出了误差函数的部分数值: 0.00 0.00000 0.05 0.05637 0.10 0.11 ...
- 并发容器之CopyOnWriteArrayList(转载)
Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容,当某个人想要修改这个内容的时候,才会真正把内容Copy出去形成一个新的内容然后再改, ...
- Centos 7中 vim 中文乱码
参考:http://www.myexception.cn/operating-system/1534005.html 一. sudo vim /etc/vimrc 在文件中加入如下几行: s ...
- 默认安装wamp修改MySQL密码
首先,通过WAMP打开mysql控制台. 提示输入密码,因为现在是空,所以直接按回车. 然后输入“use mysql”,意思是使用mysql这个数据库,提示“Database changed”就行. ...
- Dremel made simple with Parquet
http://lastorder.me/tag/parquet.html https://blog.twitter.com/2013/dremel-made-simple-with-parquet 对 ...
- 经历:easyui的layout自适应高度布局
在使用easyui的layout布局的时候,在某种情况下,我们会在后续的逻辑中修改一下layout的某个region的高度,那么该怎么做呢? 我就遇到了这样的情况,今天需求经理提出了一个需求:认证用 ...
- ios7之后 根据UILabel的文字计算frame的方法
ios7 新出来的根据label的文字和字体大小来确定label的宽高. 官方的方法是: - (CGRect)boundingRectWithSize:(CGSize)size options:(NS ...
- 在 Eclipse 中使用 JSHint 检查 JavaScript 代码
JSHint Home: http://www.jshint.com/ JSHint Options: http://www.jshint.com/options/ JSHint For Eclips ...
- java异常练习2
/*有一个圆形和长方形都可以获取面积,对于面积,如果出现非法数值,视为获取面积出现问题问题通过异常来表示.先要对这个程序进行基本的设置 *//*首先想一下,怎么写这个程序基本属性是肯定要的求面积呢?1 ...
- springmvc学习(三)
第一点---------使用 @RequestMapping 映射请求• Ant 风格资源地址支持 3 种匹配符:?:匹配文件名中的一个字符 *:匹配文件名中的任意字符 **:** 匹配多层路径 @R ...