POJ 1811 Prime Test【素数判定与大数分解】

该博客介绍了如何处理一个大整数判断其是否为素数的问题。题目要求对于给定的不超过254的整数N,确定其是否为素数并输出相应结果。如果N是素数,则输出'Prime';否则,输出N的最小质因数。博主认为这是一个经典的素数题目,解决方案可以通过使用模板实现。

题目来戳呀

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

题意:
经典的大素数题了。
是素数输出“”Prime“”,不是素数就输出最小因数。
想法:
套mr和pr的模板←_←
代码注释来自这里

#include <cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<ctime>
#include<iostream>
using namespace std;
const int s=8;//随机算法判定次数,一般8~10次就够了
typedef long long ll;
/*计算ret = (a*b)%c*/
ll mult_mod(ll a,ll b,ll c)
{
    a%=c;
    b%=c;
    ll ret=0;
    ll tem=a;
    while(b)
    {
        if(b&1)
        {
            ret+=tem;
            if(ret>c)
                ret-=c;

        }
        tem<<=1;
        if(tem>c)tem-=c;
        b>>=1;
    }
    return ret;
}
/*计算 ret = (a^n) % mod */ 
ll qpow(ll a,ll n,ll mod)
{
    ll ret=1;
    ll tem=a%mod;
    while(n)
    {
        if(n&1)
            ret=mult_mod(ret,tem,mod);
        tem=mult_mod(tem,tem,mod);//直接相乘可能会溢出
        n>>=1;
    }//因为数大所以没有完全套快速幂的模板,而是用楼上的函数改变a,n的值
    return ret;
}
/*通过 a^(n-1)=1(mod n)来判断n是不是素数  
 n-1 = x * 2^t 中间使用二次判断  
 是合数返回true,不一定是合数返回false */ 
bool check(ll a,ll n,ll x,ll t)
{
    ll ret=qpow(a,x,n);
    ll last=ret;
    for(int i=1;i<=t;++i)//进行t次(a^x % n)^2 % n  
    {
        ret=mult_mod(ret,ret,n);
        if(ret==1&&last!=1&&last!=n-1)
            return true;//合数
        last =ret;
    }
    if(ret!=1)return true;
    return false;//不一定是合数
}
/* Miller_Rabin算法  
 是素数返回true,(可能是伪素数)  
 不是素数返回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)//将n分解为x*2^t;  
    {
        x>>=1;
        t++;
    }
    srand(time(NULL));
    for(int i=0;i<s;++i)
    {
        ll a =rand()%(n-1)+1;//产生随机数a(并控制其范围在1 ~ n-1之间)  
        if(check(a,n,x,t))//是合数
            return false;
    }
    return true;
}
/* pollard_rho 算法进行质因素分解  */
int tol;//质因数的个数,编号为0~tol-1  
ll factor[100];//质因素分解结果(刚返回时是无序的) 
ll gcd(ll a,ll b)
{
    ll t;
    while(b)
    {
        t=a;
        a=b;
        b=t%b;
    }
    if(a>=0)return a;
    return -a;
}
/*求出一个因子*/
ll pollard_rho(ll x,ll c)
{
    ll i=1,k=2;
    srand(time(NULL));
    ll x0=rand()%(x-1)+1;//产生随机数x0(并控制其范围在1 ~ x-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;
        }
    }
}
/*对n进行素因子分解,存入factor。 k设置为107左右即可 */
void findfac(ll n,int k)
{
    if(n==1)return ;
    if(Miller_Rabin(n))//是素数就把这个素因子存起来  
    {
        factor[tol++]=n;
        return ;
    }
    int c=k;
    ll p=n;
    while(p>=n)
        p=pollard_rho(p,c--);//值变化,防止陷入死循环k  
    findfac(p,k);
    findfac(n/p,k);
}
int main()
{
    int T;
    ll n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&n);
        if(Miller_Rabin(n))cout<<"Prime"<<endl;
        else
        {
            tol=0;
            findfac(n,107);
            ll ans=factor[0];
            for(int i=1;i<tol;++i)
                ans=min(ans,factor[i]);
            cout<<ans<<endl;
        }
    }
    return 0;
}

ps:理解了三分?大素数判定根本就是模板题嘛,所以最后就是看敲模板的手速咯+_+

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值