poj Prime Test

本文介绍了一种高效判断大整数是否为素数的方法,并实现了若非素数则找到其最小素因子的功能。通过Miller-Rabin素性检测算法与Pollard's Rho因数分解算法,可以在较短时间内完成对特定范围内整数的素性判断及因数分解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Prime Test
Time Limit: 6000MS Memory Limit: 65536K
Total Submissions: 21644 Accepted: 4911
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


#include"stdio.h"
#include"time.h"
#include"iostream"
#include <cstdlib>
using namespace std;
int pri[]={2,3,5,7,11,13,17,19,23,29};
__int64 gcd(__int64 a,__int64 b)
{
    while(b)
    {
        __int64 c=a%b;
        a=b;
        b=c;
    }
    return a;
}
__int64 product_mod(__int64 a,__int64 b,__int64 n){
    __int64 tmp=0;
    while(b){
        if(b&1){
            tmp+=a;
            if(tmp>=n)
                tmp-=n;
        }
        a<<=1;
        if(a>=n)a-=n;
        b>>=1;
    }
    return tmp;
}
__int64 power_mod(__int64 a,__int64 m,__int64 n)
{
    __int64 tmp=1;
    a%=n;
    while(m)
    {
        if(m&1) tmp=product_mod(tmp,a,n);
        a=product_mod(a,a,n);
        m>>=1;
    }
    return tmp;
}
bool Miller_Rabin(__int64 n)
{
    if(n<2)
        return false;
    if(n==2)
        return true;
    if(!(n&1))
        return false;
    __int64 k=0,i,j,m,a;
    m=n-1;
    while(!(m&1)) m>>=1,k++;
    for(i=0;i<10;i++)
    {
        if(pri[i]>=n)
            return true;
        a=power_mod(pri[i],m,n);
        if(a==1) continue;
        for(j=0;j<k;j++)
        {
            if(a==n-1)
                break;
            a=product_mod(a,a,n);
        }
        if(j==k)
            return false;
    }
    return true;
}
__int64 pollard_rho(__int64 c,__int64 n)
{
    __int64 i,x,y,k,d;
    i=1;
    x=y=rand()%n;
    k=2;
    do{
        i++;
        d=gcd(n+y-x,n);
        if(d>1&&d<n)
            return d;
        if(i==k) y=x,k<<=1;
        x=(product_mod(x,x,n)+n-c)%n;
    }while(y!=x);
    return n;
}
__int64 rho(__int64 n)
{
    if(Miller_Rabin(n))
        return n;
    __int64 t=n;
    while(t>=n)
        t=pollard_rho(rand()%(n-1)+1,n);
    __int64 a=rho(t);
    __int64 b=rho(n/t);
    return a<b?a:b;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        __int64 n;
        scanf("%I64d",&n);
        if(Miller_Rabin(n))
            printf("Prime\n");
        else {
            printf("%I64d\n",rho(n));
        }

    }
    return 0;
}

转载于:https://www.cnblogs.com/one--world--one--dream/archive/2011/11/05/2237297.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值