博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
阅读量:7045 次
发布时间:2019-06-28

本文共 2735 字,大约阅读时间需要 9 分钟。

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

Description

Given a big integer number, you are required to find out whether it's a prime number.

Input

The first line contains the number of test cases T (1 <= T <= 20 ), then the following T lines each contains an integer number N (2 <= N < 2
54).

Output

For each test case, if N is a prime number, output a line containing the word "Prime", otherwise, output a line containing the smallest prime factor of N.

Sample Input

2510

Sample Output

Prime2

Source

 

 

 

Mean: 

 略。

analyse:

 输入的n很大,我们不可能再用筛法来求素数,这时Miller_Rabin算法就显得尤为重要。

若n不是素数,需要进行质因数分解,同样的问题,n很大,我们不可能用试除法来进行质因数分解,那样必会tle。这时就必须使用pollard_rho算法来进行质因数分解。

其实Miller_Rabin算法和pollard_rho算法很多时候是组合在一起用的。

Time complexity:O(n)  一般情况下是O(n)

 

Source code:

 

//Memory   Time// 1347K   0MS// by : Snarl_jsb#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define N 1000010#define LL long longusing namespace std;//****************************************************************// Miller_Rabin 算法进行素数测试//速度快,而且可以判断 <2^63的数//****************************************************************const int S=20; //随机算法判定次数,S越大,判错概率越小//计算 (a*b)%c. a,b都是long long的数,直接相乘可能溢出的// a,b,c <2^63long long mult_mod(long long a,long long b,long long c){ a%=c; b%=c; long long ret=0; while(b) { if(b&1){ret+=a;ret%=c;} a<<=1; if(a>=c)a%=c; b>>=1; } return ret;}//计算 x^n %clong long pow_mod(long long x,long long n,long long mod)//x^n%c{ if(n==1)return x%mod; x%=mod; long long tmp=x; long long ret=1; while(n) { if(n&1) ret=mult_mod(ret,tmp,mod); tmp=mult_mod(tmp,tmp,mod); n>>=1; } return ret;}//以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数//一定是合数返回true,不一定返回falsebool check(long long a,long long n,long long x,long long t){ long long ret=pow_mod(a,x,n); long long last=ret; for(int i=1;i<=t;i++) { ret=mult_mod(ret,ret,n); if(ret==1&&last!=1&&last!=n-1) return true;//合数 last=ret; } if(ret!=1) return true; return false;}// Miller_Rabin()算法素数判定//是素数返回true.(可能是伪素数,但概率极小)//合数返回false;bool Miller_Rabin(long long n){ if(n<2)return false; if(n==2)return true; if((n&1)==0) return false;//偶数 long long x=n-1; long long t=0; while((x&1)==0){x>>=1;t++;} for(int i=0;i
=n)p=Pollard_rho(p,rand()%(n-1)+1); findfac(p); findfac(n/p);}int main(){ //srand(time(NULL));//需要time.h头文件//POJ上G++不能加这句话 long long n; long long t; cin>>t; while(t--) { scanf("%I64d",&n); if(n==1) continue; if(Miller_Rabin(n))printf("Prime\n"); else { tol=0; findfac(n); long long minn=INT_MAX; for(int i=0;i

  

转载于:https://www.cnblogs.com/crazyacking/p/3952484.html

你可能感兴趣的文章
高性能 Web 缓存服务器 nuster 1.7.9.5 发布
查看>>
nginx对后端的目录进行反向代理
查看>>
NAT(Cisco)
查看>>
HP LaserJet Pro P1106网络打印机64位驱动安装
查看>>
SpringMVC+MyBatis整合(1)generator篇
查看>>
XMPP getting "Not Authorized" when joining an P/W protected, already open chat room
查看>>
C#设计模式之总结篇
查看>>
基于Sbo 2005B的富盛企业经营分析插件共享版免费下载
查看>>
手机上的搜索引擎-Windows Live Search Mobile 发布!
查看>>
五元组和防火墙
查看>>
Mac下添加java环境变量
查看>>
CollectionUtils工具类的常用方法
查看>>
最新勒索软件病毒防范方法及措施
查看>>
cJSON精度丢失问题
查看>>
从配置文件的格式扯到GUI和CLI
查看>>
U盘安装系统提示Ghost has detected corruption in the image解决方法
查看>>
通过Powershell重新挂接父VHD磁盘的方法
查看>>
date命令[原创]
查看>>
Rsync完全配置
查看>>
系统监控工具----Inotify-Tools
查看>>