【POJ1811】Prime Test 素数测试

AC通道:http://vjudge.net/problem/POJ-1811

【题目大意】

若n是素数,输出“Prime”,否则输出n的最小素因子,(n<=2^54)

【题解】

和bzoj3667差不多,知识这道题没那么坑。

直接上Pollord_Rho和Rabin_Miller就行了。

/*************
  POJ 1811
  by chty
  2016.11.7
*************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll INF=1000000000000000000LL;
const ll prime[10]={2,3,5,7,11,13,17,19,23};
ll T,minn;
inline ll read()
{
    ll x=0,f=1;  char ch=getchar();
    while(!isdigit(ch))  {if(ch=='-')  f=-1;  ch=getchar();}
    while(isdigit(ch))  {x=x*10+ch-'0';  ch=getchar();}
    return x*f;
}
ll gcd(ll a,ll b) {return !b?a:gcd(b,a%b);}
ll mul(ll x,ll y,ll mod) {return ((x*y-(ll)(((long double)x*y+0.5)/mod)*mod)%mod+mod)%mod;}
ll fast(ll a,ll b,ll mod) {ll sum=1;for(;b;b>>=1,a=mul(a,a,mod))if(b&1)sum=mul(sum,a,mod);return sum;}
bool Rabin_Miller(ll p,ll a)
{
    if(p==2)  return 1;
    if(!(p&1)||p==1)  return 0;
    ll d=p-1;
    while(!(d&1))  d>>=1;
    ll m=fast(a,d,p);
    if(m==1)  return 1;
    for(;d<p;d<<=1,m=mul(m,m,p)) if(m==p-1)  return 1;
    return 0;
}
bool isprime(ll x)
{
    for(ll i=0;i<9;i++)
    {
        if(prime[i]==x)  return 1;
        if(!Rabin_Miller(x,prime[i]))  return 0;
    }
    return 1;
}
void Pollord_Rho(ll x)
{
    if(isprime(x))  {minn=min(minn,x);  return;}
    ll c=3;
    while(1)
    {
        ll x1(1),x2(1),i(1),k(2);
        while(1)
        {
            x1=(mul(x1,x1,x)+c)%x;
            ll d=gcd(x1>x2?x1-x2:x2-x1,x);
            if(d>1&&d<x)  
            {
                Pollord_Rho(d);
                Pollord_Rho(x/d);
                return;
            }
            if(x1==x2)  break;
            if(++i==k)  x2=x1,k<<=1;
        }
        c++;
    }
}
void solve(ll x)
{
    if(isprime(x))  {puts("Prime"); return;}
    minn=INF;
    Pollord_Rho(x);
    printf("%lld\n",minn);
}
int main()
{
    freopen("cin.in","r",stdin);
    freopen("cout.out","w",stdout);
    T=read();
    while(T--){ll x=read();solve(x);}
    return 0;    
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值