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 ...
随机推荐
- Sublime Text3自动保存的功能(失去焦点自动保存)
这是最新版本的Sublime Text3的设置方法 (三部曲); 第一步:preferences 下面的settings:(和老版本的不一样了吧,之前有什么default 和 users,这里只有se ...
- 将centos 7改造为LINUX桌面系统
http://www.3566t.com/news/ckan/1410774.html CentOS 桌面版安装配置(以CentOS 7为例) http://blog.csdn.net/zhanghu ...
- 设置checkbox选中,设置radio选中,根据值设置checkbox选中,checkbox勾选
设置checkbox选中,设置radio选中,根据值设置checkbox选中,checkbox勾选 >>>>>>>>>>>>&g ...
- JAXB - Annotations, Controlling Element Selection: XmlAccessorType, XmlTransient
If JAXB binds a class to XML, then, by default, all public members will be bound, i.e., public gette ...
- hadoop的mapreduce过程
http://www.cnblogs.com/sharpxiajun/p/3151395.html 下面我从逻辑实体的角度讲解mapreduce运行机制,这些按照时间顺序包括:输入分片(input s ...
- Activiti源码浅析:Activity与Task
最近由于接触到Activiti工作流引擎,因此粗读了一下它的源码. 总结了一些内容如下,这些内容一般的doc上都是没有提及的. 1. model.Activity与model.Task Activit ...
- ASP中双引号单引号和&连接符使用技巧
ASP中双引号单引号和&连接符使用技巧 一.ASP中处在双引号中的可以是任意的字符.字符串,HTML代码 1.<%response.write ("I am here" ...
- mysql关键字讲解(join 、order by、group by、having、distinct)
1.join 1.1 OUTER JOIN:想要包含右侧表中的所有行,以及左侧表中有匹配记录的行. 1.11 Mysql中有左连接(left join): ...
- <Error>: CGContextRestoreGState
<Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please ...
- GCD 多线程
Grand Central Dispatch (GCD)是Apple开发的一个多核编程的较新的解决方法.它主要用于优化应用程序以支持多核处理器以及其他对称多处理系统.它是一个在线程池模式的基础上执行的 ...