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 ...
随机推荐
- luogu2678 跳石子 二分
题目大意: 一个赛道上除起点.终点外有\(N\)个点.现要求你从中删除\(M\)个点,使得剩余点序列中相邻的点的最小值最大.求这个最大的最小值. 思路 我们最容易想到的算法便是:对序列从头到尾循环\( ...
- NUnit Console Command Line
https://github.com/nunit/docs/wiki/Console-Command-Line The console interface runner is invoked by a ...
- php递归取目录下的所有文件(原创)
function get_dir_all_files($path) { $result=array(); $temp=array(); if(filetype($path)=='dir') { $di ...
- All Metro Apps on Windows 8.1 Do Not Work
所有的Metro Apps不能够正常打开,表现为打开后自动最小化到任务栏,并且不能恢复正常状态.在Event Viewer\Application中相应的错误信息为: Activation of ap ...
- CSS3实现简单的幻灯片
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 利用JavaScript制作简易日历
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <met ...
- @section Scripts{}的使用
MVC视图中,Javascripts代码被放于下面的Razor代码中(@section Scripts{}). 好处:在视图进行JavaScript编程时,是一个很好的实践,在共享视图(_Layout ...
- 杭电 1040 As Easy As A+B 【排序】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1040 解题思路:数据不大,可以直接用冒泡排序 #include<stdio.h> int ...
- RabbitMQ学习之集群模式
由于RabbitMQ是用erlang开发的,RabbitMQ完全依赖Erlang的Cluster,因为erlang天生就是一门分布式语言,集群非常方便,但其本身并不支持负载均衡.Erlang的集群中各 ...
- codeforces 789 B. Masha and geometric
链接 B. Masha and geometric depression 题意 给你一个等比数列的首项和公比q,然后给出一个上限l,m个数字,在这个等比数列里,小于l且没有在m个数字里面出现过的可以写 ...