Poj 1811 Prime Test 素数测试 Miller-Rabin 与 整数的因子分解 Pollard rho

来源:互联网 发布:加工中心圆弧编程实例 编辑:程序博客网 时间:2024/05/16 16:18

随机化算法,想尝试自己写一下,最后还是变成了抄代码。。。

代码参考了:POJ 1811 Prime Test(大素数判断和素因子分解) - kuangbin - 博客园

学习链接:

Miller-Rabin素数测试学习小计 - 将狼踩尽 19891101 - 博客园

数论部分第一节:素数与素性测试

Miller_Rabin素数测试[Fermat小定理][二次探测定理][同余式][Wilson定理] - 以中有足乐者... - 博客频道 - CSDN.NET

整数分解费马方法以及Pollard rho_龙-泪之魂_新浪博客

#include <cstdio>#include <cstring>#include <cstdlib>#include <time.h>#include <iostream>#include <algorithm>using namespace std;#define i64 __int64//****************************************************************// Miller_Rabin 算法进行素数测试//速度快,而且可以判断 <2^63的数//****************************************************************const int S=20;//随机算法判定次数,S越大,判错概率越小//计算 (a*b)%c.   a,b都是long long的数,直接相乘可能溢出的/*i64 Mult_mod (i64 a,i64 b,i64 c)   //  a,b,c <2^63{    a%=c;    b%=c;    i64 ret=0;    while (b)    {        if (b&1) {ret+=a;ret%=c;}        a<<=1;        if (a>=c)a%=c;        b>>=1;    }    return ret;}*/i64 Mult_mod (i64 a,i64 b,i64 c)  //减法实现比取模速度快{    //返回(a*b) mod c,a,b,c<2^63a%=c;b%=c;i64 ret=0;while (b){if (b&1){ret+=a;if (ret>=c) ret-=c;}a<<=1;if (a>=c) a-=c;b>>=1;}return ret;}//计算  x^n %ci64 Pow_mod (i64 x,i64 n,i64 mod) //x^n%c{    if (n==1) return x%mod;    x%=mod;    i64 tmp=x;    i64 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 (i64 a,i64 n,i64 x,i64 t){    i64 ret=Pow_mod(a,x,n);    i64 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 (i64 n){    if (n<2) return false;    if (n==2) return true;    if ((n&1)==0) return false;//偶数    i64 x=n-1;    i64 t=0;    while ((x&1)==0) {x>>=1;t++;}    for (int i=0;i<S;i++)    {        i64 a=rand()%(n-1)+1; //rand()需要stdlib.h头文件        if (Check(a,n,x,t))            return false;//合数    }    return true;}//************************************************//pollard_rho 算法进行质因数分解//************************************************i64 factor[100];//质因数分解结果(刚返回时是无序的)int tol;//质因数的个数。数组下标从0开始i64 Gcd (i64 a,i64 b){    if (a==0) return 1;  //???????    if (a<0) return Gcd(-a,b);    while (b)    {        i64 t=a%b;        a=b;        b=t;    }    return a;}i64 Pollard_rho (i64 x,i64 c){    i64 i=1,k=2;    i64 x0=rand()%x;    i64 y=x0;    while (true)    {        i++;        x0=(Mult_mod(x0,x0,x)+c)%x;        i64 d=Gcd(y-x0,x);        if (d!=1 && d!=x) return d;        if (y==x0) return x;        if (i==k) {y=x0;k+=k;}    }}//对n进行素因子分解void Findfac (i64 n){    if (Miller_Rabin(n)) //素数    {        factor[tol++]=n;        return;    }    i64 p=n;    while (p>=n) p=Pollard_rho(p,rand()%(n-1)+1);    Findfac(p);    Findfac(n/p);}int main ()  // Poj 1811 交G++ 比c++ 快很多{   // srand(time(NULL));//需要time.h头文件  //POJ上G++要去掉这句话int T;scanf("%d",&T);while (T--){i64 n;scanf("%I64d",&n);if (Miller_Rabin(n)){printf("Prime\n");continue;}tol=0;Findfac(n);i64 ans=factor[0];for (int i=1;i<tol;i++)if (factor[i]<ans)ans=factor[i];printf("%I64d\n",ans);}return 0;}


0 0