poj1811(大质数判定与大数分解的模板题)
来源:互联网 发布:域名对应多个ip 编辑:程序博客网 时间:2024/04/29 13:00
题目大意:输入一个数(<2^54),判断其是否为质数,如果否,输出其最小质因数。
经过两篇博客详解,相信这道题目完成应该没有问题了。下面附上代码。
#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;LL ans;LL modmul(LL a,LL b,LL mod){LL ret=0;for(;b;b>>=1,a=(a+a)%mod)if(b&1)ret=(ret+a)%mod;return ret;}LL qpow(LL x,LL u,LL mod){LL ret=1LL;for(;u;u>>=1,x=modmul(x,x,mod))if(u&1)ret=modmul(ret,x,mod);return ret;}LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}LL Pollard_Rho(LL n,LL c){LL i=1,j=2,x=rand()%(n-1)+1,y=x;while(1){i++;x=(modmul(x,x,n)+c)%n;LL p=gcd((y-x+n)%n,n);if(p!=1&&p!=n)return p;if(y==x)return n;if(i==j){y=x;j<<=1;}}}bool Miller_Rabin(LL n){LL x,pre,u=n-1;int i,j,k=0;if(n==2||n==3||n==5||n==7||n==11)return 1;if(n==1||!(n%2)||!(n%3)||!(n%5)||!(n%7)||!(n%11))return 0;while(!(u&1)){k++;u>>=1;}srand((long long)12234336);for(i=1;i<=50;i++){x=rand()%(n-2)+2;if(!(n%x))return 0;x=qpow(x,u,n);pre=x;for(j=1;j<=k;j++){x=modmul(x,x,n);if(x==1&&pre!=1&&pre!=n-1)return 0;pre=x;}if(x!=1)return 0;}return 1;}void find(LL n,LL c){if(n==1)return;if(Miller_Rabin(n)){ans=min(ans,n);return;}LL x=n,k=c;while(x==n)x=Pollard_Rho(x,c--);find(n/x,k);find(x,k);}int main(){int _;LL n;scanf("%d",&_);while(_--){scanf("%lld",&n);if(Miller_Rabin(n))puts("Prime");else{ans=1LL<<62;find(n,120);printf("%lld\n",ans);}}return 0;}
0 0
- poj1811(大质数判定与大数分解的模板题)
- poj1811-模板题。大数分解+大质数测试(费马+rho)
- POJ1811 大质数判断+质因数分解
- 大质数检测&&分解质因子 poj1811 Prime Test
- 大数分解与素数判定
- POJ1811 Prime Test大素数判定与因式分解
- poj1811(素数判断与合数分解模板)
- 数论之大数分解与素数判定
- 大素数分解与素数判定
- poj1811 + hdu4344 (素数测试及大数分解)
- 模板:(数论:大素数判定-分解: Miller-Rabin算法)
- MillerRabin质数判定+PollardRho质因子分解
- (Relax 数论 1.5)POJ 1811 Prime Test(MillRabin模板题+Pollard模板题:判定大素数&&合数分解)
- 分解大数质因数模板
- 大数分解模板
- 大数分解Pollard_rho模板
- poj1811(Miller-Rabin(素数测试)与Pollard rho(整数的因子分解))
- [POJ 1811 Prime Test] Miller_Rabin + Pollard_rho 大数质数判断/质因子分解模板
- 经典的c++面试题
- 谈一谈对JS闭包的理解
- 函数指针、指针函数类
- 103. Binary Tree Zigzag Level Order Traversal
- LeetCode - Container With Most Water
- poj1811(大质数判定与大数分解的模板题)
- Leetcode #9 Palindrome Number
- Ubuntu16.04 + cuda8.0 + GTX1080 + matlab14.04a + Opencv3.0 + caffe 安装教程
- Delphi中,让程序只运行一次的方法
- 面试总结
- 设计模式-类图和对象图
- 常用排序算法之归并排序
- X264中的sad-a.asm
- struts2 存取cookie