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 ...
随机推荐
- Eclipse中的TreeViewer类和ListViewer类
TreeViewer和TableViewer在使用上还是有很多相似之处.TreeViewer中冶有TableViewer中的过滤器和排序器.具体使用看TableViewer中的使用. 和Table有J ...
- BeanFactory与FactoryBean
1. BeanFactory BeanFactory定义了 IOC 容器的最基本形式,并提供了 IOC 容器应遵守的的最基本的接口,也就是Spring IOC 所遵守的最底层和最基本的编程规范.在 ...
- nodejs中npm工具自身升级
npm官方地址:https://www.npmjs.com/ npm在github上的地址:https://github.com/npm/npm 目前(截止2016-03-15)npm最新版本为3.8 ...
- asp发布至IIS
最近做的一个任务是使用asp写的,显示的是数据库的报表,但是将报表当前目录发布至IIS后,发现还是运行不了 Set conn = Server.CreateObject("ADODB.Con ...
- css3边框、阴影
一.图像边框border-image 语法:border-image:border-image-source(图片) || border-image-slice(裁剪位置) || border ...
- 正则表达式 自学笔记整理记录 EASY REGEX~
整理正则表达式篇 -wuian7yulian 基础知识介绍 : 1> 字符串的组成: 对于字符串"ABC"来说,其包 ...
- 冒泡排序小实例 php
源代码如下,仅用于参考: <?php$a = array(10,2,36,14,10,25,23,85,99,45); for($j=0;$j<9;$j++){ for($i=0;$i&l ...
- 在ASP中限制同一表单被多次提交
本文介绍在ASP应用中防止用户在当前会话期间多次提交同一表单的一个简单方法.它主要由四个子程序组成,在较为简单的应用场合,你只要将这些代码放在包含文件中直接引用即可:对于那些较为复杂的环境,我们在文章 ...
- Linux find常见用法示例
find命令的参数: pathname: find命令所查找的目录路径.例如用.来表示当前目录,用/来表示系统根目录.-print: find命令将匹配的文件输出到标准输出.-exec: find命令 ...
- 修改Latex常用编辑器WinEdt中的字号与字体 [转]
用latex编写科技文章已经是大多数科研工作者采用的方法,其编写效果是word所远不能及的.但是其效果只是在编译之后,之前文字那弱小的身躯确实令很多人无奈.10Pt的字体在以前14寸的电脑上看起来还是 ...