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

你可能感兴趣的文章
打印左下三角形
查看>>
关于学术诚信与职业道德的承诺
查看>>
Windows Server 2008搭建域控制器《转载51CTO.com》
查看>>
配置本地yum源的方法
查看>>
linux下android开发环境搭建及NDK安装(转)
查看>>
C++构造函数
查看>>
Apache Hive 安装文档
查看>>
Raspberry Pi(树莓派)国内软件源
查看>>
dev 中 字符串转中文拼音缩写,对grid列表进行模糊匹配,grid获取焦点行,gridlookupedit控件用拼音模糊匹配下拉选项...
查看>>
转.Buffer Management by the Camera Driver (Windows Embedded CE 6.0)
查看>>
仿金蝶-记账凭证
查看>>
jquery自动焦点图
查看>>
网络传输机制
查看>>
【HDOJ】1057 A New Growth Industry
查看>>
在windows和unbuntu上安装octave
查看>>
codeforces727F Polycarp's problems(dp)
查看>>
Microsoft .Net Remoting系列专题之一:.Net Remoting基础篇
查看>>
做WinForm项目时遇到的一个问题,MessageBox不从用户控件那一层弹出
查看>>
与正则有关的JS方法结合其在项目中的应用
查看>>
CentOS 7.6最小化安装(系统盘和数据盘分离安装)
查看>>