POJ1811 Prime Test 【大素数判断及找最小素因子】

Prime Test
Time Limit: 6000MS Memory Limit: 65536K
Total Submissions: 30007 Accepted: 7672
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 < 2 54).

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
 
Miller_Rabin()
Pollard_Rho()
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;  
#define LL long long 
#define MAX ((long long)1<<61)

LL factor[200],cnt;
LL mini; 

LL gcd(LL a,LL b){  
    return (b==0)?a:gcd(b,a%b);  
}  
LL Mulmod(LL a,LL b,LL n)	// a * b % n
{  
    LL  exp = a%n, res = 0;  
    while(b)  
    {  
        if(b&1)  
        {  
            res+=exp;  
            if(res>n) res-=n;  
        }  
        exp <<= 1;  
        if(exp>n)  
            exp-=n;  
        b>>=1;  
    }  
    return res;  
}  
  
LL exp_mod(LL a,LL b,LL c)	// a ^ b % c
{  
    LL k = 1;  
    while(b)  
    {  
        if(b&1)  
            k = Mulmod(k,a,c);  
        a = Mulmod(a,a,c);  
        b>>=1;  
    }  
    return k;  
}  
bool Miller_Rabin(LL n, LL times)	// 判断n是否为素数,time一般取10 
{  
    if(n==2)return 1;  
    if(n<2||!(n&1))return 0;  
  
    LL a, u=n-1, x, y;  
    int t=0;  
    while(u%2==0){  
        t++;  
        u/=2;  
    }  
    srand(100);  
    for(int i=0;i<times;i++)  
    {  
        a = rand() % (n-1) + 1;  
        x = exp_mod(a, u, n);  
        for(int j=0;j<t;j++)  
        {  
            y = Mulmod(x, x, n);  
            if ( y == 1 && x != 1 && x != n-1 )  
                return false; //must not  
            x = y;  
        }  
        if( y!=1) return false;  
    }  
    return true;  
}  
  
LL Pollard_Rho(LL n,LL c)  
{  
    LL x,y,d,i=1,k=2;  
    y = x = rand() % (n-1) + 1;  
    while(1)  
    {  
        i++;  
        x = (Mulmod(x,x,n) + c)%n;  
        d = gcd((x-y+n)%n,n);  
        if(d>1&&d<n)  
            return d;  
        if(x==y)  
            return n;  
        if(i==k)  
        {  
            k<<=1;  
            y = x;  
        }  
    }  
}  
 
void Find_factor(LL n,LL c)  
{  
    if(n==1)  
        return;  
    if(Miller_Rabin(n,10))  
    {  
        factor[cnt++] = n; 
        if(n<mini) mini=n; 
        return;  
    }  
    LL p = n;  
    LL k = c;  
    while(p>=n)  
        p = Pollard_Rho(p,c--);  
    Find_factor(p,k);  
    Find_factor(n/p,k);  
}  

int main()  
{
    int t;
    scanf("%d",&t);
    LL n;
    while(t--){
        scanf("%lld",&n);
        mini=MAX;
        cnt=0;
        if(Miller_Rabin(n,10)) puts("Prime");
        else{
            Find_factor(n,120);
            printf("%lld\n",mini);
        }
    }
  return 0; 
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值