poj 1811 Prime Test(拉宾米勒测试+大数分解)

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



给定一个数 判断是否是素数 如果不是素数给出最小的素因子

由于 给出的数可能会很大 所以 不能用普通的方法 在这里用到的就是拉宾米勒测试+pollard_rho

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#include <ctime>
#include <cstdlib>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1010
#define MAXM 100010
#define INF 99999999
#define ll long long
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
#define MAX ((ll)1<<61)
#define Times 10
#define C 201
using namespace std;

int Read()
{
    char ch;
    int a = 0;
    while((ch = getchar()) == ' ' | ch == '\n');
    a += ch - '0';
    while((ch = getchar()) != ' ' && ch != '\n')
    {
        a *= 10;
        a += ch - '0';
    }
    return a;
}

void Print(int a)    //Êä³öÍâ¹Ò
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}
ll mini;
int cnt;
ll pri[MAXN];
ll gcd(ll a,ll b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
ll random(ll n)
{
    return (ll)((double)rand()/RAND_MAX*n+0.5);
}
ll mult(ll a,ll b,ll m)//a*b%m
{
    ll ret=0;
    while(b>0)
    {
        if(b&1)
            ret=(ret+a)%m;
        b>>=1;
        a=(a<<1)%m;
    }
    return ret;
}
ll quick_mod(ll a,ll b,ll m)
{
    ll ans=1;
    a%=m;
    while(b)
    {
        if(b&1)
        {
            ans=mult(ans,a,m);
            b--;
        }
        b/=2;
        a=mult(a,a,m);
    }
    return ans;
}
bool Witness(ll a,ll n)
{
    ll m=n-1;
    int j=0;
    while(!(m&1))
    {
        j++;
        m>>=1;
    }
    ll x=quick_mod(a,m,n);
    if(x==1||x==n-1)
        return 0;
    while(j--)
    {
        x=x*x%n;
        if(x==n-1)
            return 0;
    }
    return 1;
}

bool miller_rabin(ll n)
{

//    cout<<"n "<<n<<endl;
    if(n<2) return 0;
    if(n==2) return 1;
    if(!(n&1))  return 0;
    for(int i=1;i<=Times;i++)
    {
        ll a=random(n-2)+1;
        if(Witness(a,n))  return 0;
    }
    return 1;
}
ll pollard_rho(ll n,int c)
{
    ll x,y,d,i=1,k=2;
    x=random(n-1)+1;
    y=x;
    while(1)
    {
        i++;
        x=(mult(x,x,n)+c)%n;
        d=gcd(y-x,n);
        if(1<d&&d<n)
            return d;
        if(y==x)
            return n;
        if(i==k)
        {
            y=x;
            k<<=1;
        }
    }
}
void find(ll n,int k)
{
    if(n==1)
        return;
    if(miller_rabin(n))
    {
        pri[++cnt]=n;
        if(n<mini)
            mini=n;
        return;
    }
    ll p=n;
    while(p>=n)
        p=pollard_rho(p,k--);
    find(p,k);
    find(n/p,k);
}


int main()
{
    //fread;
    int tc;
    scanf("%d",&tc);
    while(tc--)
    {
        ll n;
        scanf("%lld",&n);
        mini=MAX;
        cnt=0;
        if(miller_rabin(n))
            puts("Prime");
        else
        {
            find(n,C);
            printf("%lld\n",mini);
        }
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值