POJ-1811--Prime Test(Miller-Rabin算法判断素数+最小素因子模板)

Time Limit: 6000MS Memory Limit: 65536K
Total Submissions: 39911 Accepted: 10758
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 < 254).

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

2
5
10

Sample Output

Prime
2

Source

POJ Monthly

 

C++:

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<ctime>
using namespace std;
typedef long long ll;//以后出现的ll为ll 类型
const int Ss=8;
ll n;

ll mult_mod(ll a,ll b,ll c)
{
    a%=c;
    b%=c;
    ll  ret=0;
    ll tmp=a;
    while(b)
    {
        if(b&1)
        {
            ret+=tmp;
            if(ret>c)
                ret-=c;
        }
        tmp<<=1;
        if(tmp>c)
            tmp-=c;
        b>>=1;
    }
    return ret;
}
ll pow_mod(ll a,ll n,ll mod)
{
    ll ret=1;
    ll temp=a%mod;
    while(n)
    {
        if(n&1)
            ret=mult_mod(ret,temp,mod);
        temp=mult_mod(temp,temp,mod);
        n>>=1;
    }
    return ret;
}

bool check_miller(ll a,ll n,ll x,ll t)
{
    ll ret=pow_mod(a,x,n);
    ll 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;
    else
        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 ++;
    }
    srand(time(NULL));

    for(int i=0; i<Ss; ++i)
    {
        ll a=rand()%(n-1)+1;
        if(check_miller(a,n,x,t))
        {
            return false;
        }
    }
    return true;
}

ll factor[110];
int tol;
ll gcd(ll a,ll b)
{
    ll t;
    while(b)
    {
        t=a;
        a=b;
        b=t%b;
    }
    if(a>=0)
        return a;
    else
        return -a;
}
ll pollard_rho(ll x,ll c)
{
    ll i=1,k=2;
    srand(time(NULL));
    ll x0=rand()%(x-1)+1;
    ll y=x0;
    while(1)
    {
        i++;
        x0=(mult_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,int k)
{
    if(n==1)
        return ;
    if(Miller_Rabin(n))
    {
        factor[tol++]=n;
        return ;
    }
    ll p=n;
    int c=k;
    while(p>=n)
        p=pollard_rho(p,c--);
    findfac(p,k);
    findfac(n/p,k);
}

int main()
{
    int N;
    scanf("%d",&N);
    while(N--)
    {
        scanf("%I64d",&n);
        if(Miller_Rabin(n))
            printf("Prime\n");
        else
        {
            tol=0;
            findfac(n,107);
            ll ans=factor[0];
            for(int i=1; i<tol; ++i)
            {
                ans=min(ans,factor[i]);
            }

            printf("%I64d\n",ans);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值