POJ 1811
使用Pollard_rho算法就可以过了
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stdlib.h>
#include <time.h>
#define LL __int64
using namespace std;
LL ans;
const LL C=201;
LL random(LL n){
return (LL)((double)rand()/RAND_MAX*n+0.5);
} LL gcd(LL a,LL b){
if(b==0) return a;
return gcd(b,a%b);
} LL multi(LL a,LL b,LL m){ a*b%m这个函数写得真心好,很好地避免了超出范围的情 况
LL ret=0;
while(b>0){
if(b&1)
ret=(ret+a)%m;
b>>=1;
a=(a<<1)%m;
}
return ret;
} LL Pollard_rho(LL n, LL c){
LL x,y,d,i=1,k=2;
x=random(n-1)+1;
y=x;
while(true){
i++;
x=(multi(x,x,n)+c)%n;
d=gcd(y-x,n);
if(d>1&&d<n) return d;
if(y==x) return n;
if(i==k){
y=x;
k=k<<1;
}
}
} LL quick(LL a,LL k,LL m){
LL ans=1;
a%=m;
while(k){
if(k&1){
ans=multi(ans,a,m);
}
k=k>>1;
a=multi(a,a,m); // 这里如果不写函数直接乘会超范围
}
return ans;
} bool Witness(LL a, LL n){
LL m=n-1;
int j=0;
while(!(m&1)){
j++;
m=m>>1;
}
LL x= quick(a,m,n);
if(x==1||x==n-1)
return false;
while(j--){
x=multi(x,x,n);
if(x==n-1)
return false;
}
return true;
} bool Miller_Rabin(LL n){
if(n<2) return false;
if(n==2) return true;
if(!(n&1)) return false;
for(int i=1;i<=10;i++){
LL a=random(n-2)+1;
if(Witness(a,n)) return false;
}
return true;
} void find(LL n){
if(n==1) return ;
if(Miller_Rabin(n)){
if(n<ans)
ans=n;
return ;
}
LL p=n;
while(p>=n)
p=Pollard_rho(p,random(n-2)+1);
find(p);
find(n/p);
} int main(){
LL n; int T;
srand(time(0));
scanf("%d",&T);
while(T--){
scanf("%I64d",&n);
if(Miller_Rabin(n)){
printf("Prime\n");
continue;
}
ans=(1LL<<60);
find(n);
printf("%I64d\n",ans);
}
return 0;
}
POJ 1811的更多相关文章
- 数学#素数判定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 ...
- 数论 - 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 ...
- poj 1811 Prime Test 大数素数测试+大数因子分解
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 27129 Accepted: 6713 Case ...
- Miller&&Pollard POJ 1811 Prime Test
题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************** ...
- POJ 1811 大素数判断
数据范围很大,用米勒罗宾测试和Pollard_Rho法可以分解大数. 模板在代码中 O.O #include <iostream> #include <cstdio> #inc ...
- 大素数测试 求因子 poj 1811
抄别人的 #include<stdio.h> #include<string.h> #include<algorithm> #include<stdlib.h ...
随机推荐
- ios开发之Swift新手入门
1.关于swift和调试,swift在ios7.0才支持,ios8.3系统的真机必需要xcode6.3才干调试.安装xcode6.3需要os x 10.10以上 2.应用程序由Main.storybo ...
- Objective-c基础知识学习笔记
Objective-c基础知识学习笔记(一) 一直有记录笔记的习惯.但非常久没分享一些东西了,正好上半年開始学习IOS了,如今有空写点.因开发须要,公司特意为我们配置了几台新MAC.还让我们自学了2周 ...
- Linux - xshell 链接CentOS 设置高亮
默认是黑白的! 用了vim 指令还是黑白的. 两种途径设置,一种是通过Alt+P. 一种是选择配色方案来设置.
- 【SDOI 2008】 仪仗队
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2190 [算法] 同POJ3090 值得注意的是此题数据规模较大,建议使用用线性筛筛出 ...
- 10.2 Hibernate持久层
点击项目右键->MyEclipse->Add Hibernate Capabilities 打开MyEclipse Hibernate Perspective(MyEclipse Hibe ...
- [HTML] 如何使用robots.txt防止搜索引擎抓取页面
Robots.txt 文件对抓取网络的搜索引擎漫游器(称为漫游器)进行限制.这些漫游器是自动的,在它们访问网页前会查看是否存在限制其访问特定网页的 robots.txt 文件.如果你想保护网站上的某些 ...
- SpringBoot中拦截器和过滤器的使用
一.拦截器 三种方式 继承WebMvcConfigurerAdapter spring5.0 以弃用,不推荐 实现WebMvcConfigurer 推荐 继承WebMvcConfiguratio ...
- Cracking the Coding Interview 5.2
Given a(decimal -e.g. 3.72)number that is passed in as a string, print the binary representation. If ...
- 使用Axis2方式发布webService实例说明
1.简单的pojo方式: 不需要写配置文件,直接把class文件拷贝到axis2的WEB-INF目录下的poji文件夹下即可.但其局限性表现在,实现类不能有包声明,这在实际开发过程中使用较少,这里不做 ...
- C#自定义控件实现控件随窗口大小改变
1.新建用户控件,取名MyForm. 2.将默认的UserControl改成Form 3.在类中添加以下代码 private float X, Y; //获得控件的长度.宽度.位置.字体大小的数据 p ...