POJ-1811(Miller Rabin素性测试+Pollard_rho分解质因数)

题意:给出一个数N(2 <= N < 2 54),判断N是否是素数,如果是素数,直接输出"Prime",反之,输出N的最小质因数。

//先粘一下正解,等等再完善

#include <iostream>
#include <ctime>
#include <algorithm>
#define LL long long
const int S=20;
LL factor[100];
int tot;
LL n;
using namespace std;
 
LL quickmult(LL a,LL b,LL mod)
{
    a%=mod;
    b%=mod;
    LL ans=0;
    while(b)
    {
        if(b&1)
        {
            ans+=a;
            ans%=mod;
        }
        b/=2;
        a=(a+a)%mod;
    }
    return ans;
}
LL quickpow(LL a,LL b,LL mod)
{
    LL ans=1;
    while(b)
    {
        if(b&1)
        {
            ans=quickmult(ans,a,mod)%mod;
        }
        b/=2;
        a=quickmult(a,a,mod)%mod;
    }
    return ans;
    /*if(n==1)
        return a%mod;
    LL temp=a;
    LL ret=1;
    while(b)
    {
        if(b&1)
            ret=quickmult(ret,temp,mod);
        temp=quickmult(temp,temp,mod);
        b/=2;
    }
    return ret;*/
}
bool check(LL a,LL n,LL x,LL T)
{
    LL temp1=quickpow(a,x,n);
    LL temp2=temp1;
    while(T--)
    {
        temp1=quickmult(temp1,temp1,n);
        if(temp1==1&&temp2!=1&&temp2!=n-1)
            return true;
        temp2=temp1;
    }
    if(temp1!=1)
        return true;
    return false;
}
bool Miller_Rabin(LL n)
{
    if(n<2)
        return false;
    if(n==2)
        return true;
    if((n&1)==0)
        return false;
    LL x=n-1;
    LL t=0;
    while((x&1)==0)
    {
        x>>=1;
        t++;
    }
    for(int i=0;i<S;i++)
    {
        LL a=rand()%(n-1)+1;
        if(check(a,n,x,t))
            return false;
    }
    return true;
}
LL gcd(LL a,LL b)
{
   /* if(b==0)
        return a;
    return gcd(b,a%b);*/
    if(a==0)
        return 1;
    if(a<0)
        return gcd(-a,b);
    while(b)
    {
        LL temp=a%b;
        a=b;
        b=temp;
    }
    return a;
}
LL Pollard_rho(LL x,LL c)
{
    LL i=1,k=2;
    LL x0=rand()%x;
    LL y=x0;
    while(true)
    {
        
        i++;
        x0=(quickmult(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)
{
    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()
{
    int T;
    while(cin>>T)
    {
        while(T--)
        {
            cin>>n;
            tot=0;
            findfac(n);
            if(tot==1)
                cout<<"Prime"<<endl;
            else
            {
                sort(factor,factor+tot);
                cout<<factor[0]<<endl;
            }
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值