POJ 1811 Prime Test(大素数判断和素因子分解)

来源:互联网 发布:成为最好的自己 知乎 编辑:程序博客网 时间:2024/05/22 05:22

题目链接:

题目大意:

给出一个N(2<=N<2^54)如果是素数,输出Prime,否则输出N的最小素因子。
思路:(miller素数判断&&pollar_rho大数分解)

模板题

kuangbin的模板

数据比较大,只能先用Miller_Rabin算法进行素数判断。
在用Pollard_rho分解因子。
随机算法非常厉害~~~~~Orz
 
附上两个程序,作为模板:
 
程序一:POJ上只能用G++不能加srand();

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<time.h>#include<iostream>#include<string.h>#include<math.h>#include<algorithm>using 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<S;i++)    {        long long a=rand()%(n-1)+1;//rand()需要stdlib.h头文件        if(check(a,n,x,t))            return false;//合数    }    return true;}//************************************************//pollard_rho 算法进行质因数分解//************************************************long long factor[100];//质因数分解结果(刚返回时是无序的)int tol;//质因数的个数。数组小标从0开始long long gcd(long long a,long long b){    if(a==0)return 1;//???????    if(a<0) return gcd(-a,b);    while(b)    {        long long t=a%b;        a=b;        b=t;    }    return a;}long long Pollard_rho(long long x,long long c){    long long i=1,k=2;    long long x0=rand()%x;    long long y=x0;    while(1)    {        i++;        x0=(mult_mod(x0,x0,x)+c)%x;        long long 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(long long n){    if(Miller_Rabin(n))//素数    {        factor[tol++]=n;        return;    }    long long p=n;    while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1);    findfac(p);    findfac(n/p);}int main(){   // srand(time(NULL));//需要time.h头文件  //POJ上G++要去掉这句话    int T;    long long n;    scanf("%d",&T);    while(T--)    {        scanf("%I64d",&n);        if(Miller_Rabin(n))        {            printf("Prime\n");            continue;        }        tol=0;        findfac(n);        long long ans=factor[0];        for(int i=1;i<tol;i++)          if(factor[i]<ans)             ans=factor[i];        printf("%I64d\n",ans);    }    return 0;}

程序二:

C++

#include<stdio.h>#include<string.h>#include<iostream>#include<math.h>#include<stdlib.h>#include<time.h>using namespace std;typedef long long LL;#define maxn 10000LL factor[maxn];int tot;const int S=20;LL muti_mod(LL a,LL b,LL c){    //返回(a*b) mod c,a,b,c<2^63    a%=c;    b%=c;    LL 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;}LL pow_mod(LL x,LL n,LL mod){  //返回x^n mod c ,非递归版    if (n==1) return x%mod;    int bit[90],k=0;    while (n){        bit[k++]=n&1;        n>>=1;    }    LL ret=1;    for (k=k-1;k>=0;k--){        ret=muti_mod(ret,ret,mod);        if (bit[k]==1) ret=muti_mod(ret,x,mod);    }    return ret;}bool check(LL a,LL n,LL x,LL t){   //以a为基,n-1=x*2^t,检验n是不是合数    LL ret=pow_mod(a,x,n),last=ret;    for (int i=1;i<=t;i++){        ret=muti_mod(ret,ret,n);        if (ret==1 && last!=1 && last!=n-1) return 1;        last=ret;    }    if (ret!=1) return 1;    return 0;}bool Miller_Rabin(LL n){    LL x=n-1,t=0;    while ((x&1)==0) x>>=1,t++;    bool flag=1;    if (t>=1 && (x&1)==1){        for (int k=0;k<S;k++){            LL a=rand()%(n-1)+1;            if (check(a,n,x,t)) {flag=1;break;}            flag=0;        }    }    if (!flag || n==2) return 0;    return 1;}LL gcd(LL a,LL b){    if (a==0) return 1;    if (a<0) return gcd(-a,b);    while (b){        LL t=a%b; a=b; b=t;    }    return a;}LL Pollard_rho(LL x,LL c){    LL i=1,x0=rand()%x,y=x0,k=2;    while (1){        i++;        x0=(muti_mod(x0,x0,x)+c)%x;        LL d=gcd(y-x0,x);        if (d!=1 && d!=x){            return d;        }        if (y==x0) return x;        if (i==k){            y=x0;            k+=k;        }    }}void findfac(LL n){           //递归进行质因数分解N    if (!Miller_Rabin(n)){        factor[tot++] = n;        return;    }    LL p=n;    while (p>=n) p=Pollard_rho(p,rand() % (n-1) +1);    findfac(p);    findfac(n/p);}int main(){   // srand(time(NULL));//POJ上G++要去掉这句话    int T;    scanf("%d",&T);    long long n;    while(T--)    {        scanf("%I64d",&n);        if (!Miller_Rabin(n)) {printf("Prime\n"); continue; }        tot = 0;        findfac(n);        long long ans=factor[0];        for(int i=1;i<tot;i++)          if(factor[i]<ans)ans=factor[i];        printf("%I64d\n",ans);    }    return 0;}


阅读全文
0 0
原创粉丝点击