使用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的更多相关文章

  1. 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429

    素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...

  2. 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 ...

  3. Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test

    POJ 1811 Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 32534   Accepted: 8 ...

  4. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  5. poj 1811 大数分解

    模板 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> ...

  6. poj 1811 Pallor Rho +Miller Rabin

    /* 题目:给出一个数 如果是prime 输出prime 否则输出他的最小质因子 Miller Rabin +Poller Rho 大素数判定+大数找质因子 后面这个算法嘛 基于Birthday Pa ...

  7. poj 1811 Prime Test 大数素数测试+大数因子分解

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 27129   Accepted: 6713 Case ...

  8. Miller&&Pollard POJ 1811 Prime Test

    题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************** ...

  9. POJ 1811 大素数判断

    数据范围很大,用米勒罗宾测试和Pollard_Rho法可以分解大数. 模板在代码中 O.O #include <iostream> #include <cstdio> #inc ...

  10. 大素数测试 求因子 poj 1811

    抄别人的 #include<stdio.h> #include<string.h> #include<algorithm> #include<stdlib.h ...

随机推荐

  1. springAOP注解方式实现日志操作

    通过自定义注解调用方法执行日志存储: package com.zktx.platform.log2; import java.lang.reflect.Method; import java.util ...

  2. 在Android系统中添加宏控制代码【原创】

    关键词:Android  宏控制 Androd.mk 平台信息:内核:linux3.4.39 系统:android/android5.1平台:S5P4418  作者:庄泽彬(欢迎转载,请注明作者) 邮 ...

  3. day63-webservice 09.jquery调用ajax

    WebService可以有很多种调用方式,除了之前说的,还可以有jquery.拿原生的Ajax做调用,拿jquery怎么调用啊?原生的能调,jquery指定也能调.原生的Ajax是通过网页直接点HTM ...

  4. LINUX/UNIX找回删除的文件

    当Linux计算机受到入侵时,常见的情况是日志文件被删除,以掩盖攻击者的踪迹.管理错误也可能导致意外删除重要的文件,比如在清理旧日志时,意外地删除了数据库的活动事务日志.有时可以通过lsof来恢复这些 ...

  5. php获取前天的昨天的日期

    在PHP里得到前天和昨天的日期的代码前天去面试的时候也是这样,不过我当时记不起来了.就记得MYSQL里面的date_sub(now(),'interval 1 day');date('Y/m/d h: ...

  6. java网络通信编程

    网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴.在发送和接收数据时,大部分的程序设计语言都设 ...

  7. python字符串中的单双引

    python中字符串可以(且仅可以)使用成对的单引号.双引号.三个双引号(文档字符串)包围: 'this is a book' "this is a book" "&qu ...

  8. .net中的母版页中使用FindControl的使用

    前几天,遇到一个字段比较多的用户填写的页面(数据库表中就将近100个字段),怎么讲这些input的标签的值,保存数据库了?(使用的是母版页下面的aspx,不包括前段获取input的值,传给后台) 作为 ...

  9. centos6.6--------反向DNS配置

    一.反向区: 将域名解析为IP====================================================================================注 ...

  10. 使用Micrisoft.net设计方案 第一章 企业解决方案中构建设计模式

    第一章企业解决方案中构建设计模式 我们知道的系统总是由简单到复杂,而不是直接去设计一个复杂系统.如果直接去设计一个复杂系统,结果最终会导致失败.在设计系统的时候,先设计一个能够正常工作的系统,然后在此 ...