使用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. 如何将dmp文件导入到自己的oracle数据库中

    1.首先,我们可以先建立自己的一个用户表空间,创建表空间的格式如下: create tablespace test(表空间的名字) datafile 'D:\oracle\product\10.2.0 ...

  2. POJ3349 Language: Snowflake Snow Snowflakes

    POJ3349 Language: Snowflake Snow Snowflakes 题目:传送门 题解: 链表+hash的一道水题 填个坑补个漏... 代码: #include<cstdio ...

  3. 根据日期获取,x岁x月x天

    c#: DateTime startDate = new DateTime(); DateTime endDate = new DateTime(); ; ; ; if (endDate.Month& ...

  4. if,elif,else的关系 input print int的用法

    qian=input("找劳保网是什么网站?:")if qian=="zhaolaobaowang.com": print("正确")els ...

  5. 48.自用qss

    /* R1 */ QDialog { background-image: url(:/images/background.png); } /* R2 */ QLabel { font: 9pt; co ...

  6. RMS:均方根值,RMSE:均方根误差,MSE:标准差

    .均方根值(RMS),有时也称方均根.效值.英语写为:Root Mean Square(RMS). 美国传统词典的定义为:The square root of the average of squar ...

  7. 5个对话框和FileStream:文件流

    1.private void button1_Click(object sender, EventArgs e) { colorDialog1.ShowDialog();//显示颜色选择器 panel ...

  8. hdu2680 Choose the best route 最短路(多源转单源)

    此题中起点有1000个,边有20000条.用链式前向星建图,再枚举起点用SPFA的话,超时了.(按理说,两千万的复杂度应该没超吧.不过一般说计算机计算速度 1~10 千万次/秒.也许拿最烂的计算机来卡 ...

  9. 关于生sql中的空值

    在数据库中的空值无非就是两种形式的表示 一种是什么都没有,一种是以NuLL显示的 , 那么在C# 读取出来怎么判断呢, DtStatus.Rows[0]["FetchCode"]. ...

  10. div 内容水平垂直居中

    对于前端布局来说.总有一些图片水平垂直居中老是不好看,影响整体美观,百度一大堆各种自适应方法,终于找到了一种比较简单,适用于所有场景的方法.. 1.对于布局来说.一个div搞定. <div id ...