poj 1811: Prime Test

题目链接

涉及到大数的素性判定及大数的质因数分解,感觉用处不大。当成一块模板好了

#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
#define abs(a) (a)>=0? (a):(-a)

const int S=20;

LL qmut(LL a,LL b,LL mod)
{
    a%=mod,b%=mod;
    LL ret=0;
    for(;b;b>>=1)
    {
        if(b&1) ret=(ret+a)%mod;
        a=(a+a)%mod;
    }
    return ret;
}
LL qpow(LL x,LL n,LL mod)
{
    LL ret=1;
    for(;n;n>>=1)
    {
        if(n&1) ret=qmut(ret,x,mod);
        x=qmut(x,x,mod);
    }
    return ret;
}
LL gcd(LL a,LL b)
{
    a=abs(a),b=abs(b);
    return b? gcd(b,a%b):a;
}

LL fac[105],tot;

bool check(LL a,LL n,LL x,LL t)
{
    LL ret=qpow(a,x,n),last=ret;
    for(int i=1;i<=t;i++)
    {
        ret=qmut(ret,ret,n);
        if(ret==1&&last!=1&&last!=n-1) return true;
        last=ret;
    }
    if(ret!=1) return true;
    return false;
}

bool Miller_Rabin(LL n)
{
    LL x=n-1,t=0;
    while((x&1)==0) x>>=1,t++;
    bool flag=true;
    if(t>=1 && (x&1)==1)
        for(int k=0;k<S;k++)
        {
            LL a=rand()%(n-1)+1;
            if(check(a,n,x,t))
            {
                flag=true;
                break;
            }
            flag=false;
        }
    if(!flag||n==2) return 0;
    return true;
}

LL Pollard_rho(LL x,LL c)
{
    LL i=1,x0=rand()%x,y=x0,k=2;
    while(1)
    {
        i++;
        x0=(qmut(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;
    }
}
void get(LL n)
{
    if(!Miller_Rabin(n))
    {
        fac[tot++]=n;
        return ;
    }
    LL p=n;
    while(p>=n)    p=Pollard_rho(p,rand()%(n-1)+1);
    get(p);
    get(n/p);
}

int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        LL n;
        scanf("%lld",&n);
        if(!Miller_Rabin(n))
        {
            puts("Prime");
            continue;
        }
        tot=0;
        get(n);
        LL ans=*min_element(fac,fac+tot);
        printf("%lld\n",ans);
    }
}
时间: 2024-08-11 05:44:51

poj 1811: Prime Test的相关文章

数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

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

Miller&amp;&amp;Pollard POJ 1811 Prime Test

题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************************************************ * Author :Running_Time * Created Time :2015-8-28 13:02:38 * File Name :POJ_1811.cpp ************************************

POJ 1811 Prime Test(费马小定理+二次探测定理)

素数的测试: 费尔马小定理:如果p是一个素数,且0<a<p,则a^(p-1)%p=1. 利用费尔马小定理,对于给定的整数n,可以设计素数判定算法,通过 计算d=a^(n-1)%n来判断n的素性,当d!=1时,n肯定不是素数,当d=1时,n   很可能是素数. 二次探测定理:如果n是一个素数,且0<x<p,则方程x^2%p=1的解为:x=1或    x=p-1. 利用二次探测定理,可以再利用费尔马小定理计算a^(n-1)%n的过程 中增加对整数n的二次探测,一旦发现违背二次探测条件,

Prime Test POJ - 1811(素性检测+大数分解)

Prime Test POJ - 1811 题意:判断N (2 <= N < 2 54) 是不是素数,如果不是求它的最小素因数. millerRabin素性检测 + pollard rho大数分解 链接 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 #define LL l

数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&amp;2429

素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: http://blog.csdn.net/maxichu/article/details/45459533 然后是参考了kuangbin的模板: http://www.cnblogs.com/kuangbin/archive/2012/08/19/2646396.html 模板如下: //快速乘 (a

poj 1811, poj 2429 (pollard_rho算法)

poj 1811 题意: 给出一个整数n,判断n是不是素数,如果不是素数,输出最小的质因子. 限制: 2 <= N < 2^54 思路: miller_rabin算法判素数 pollard_rho算法求质因子 复杂度O(log(n)) poj 2429 题意: 给出两个数的lcm和gcd,求这两个数. 限制: 0 < lcm,gcd < 2^63 思路: pollard_rho O(log(n))分解质因数. 可以考虑到2^63不同的质因数只有20左右个,而相同的质数不可能分在不同

POJ 1365 Prime Land

Prime Land Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2972 Accepted: 1362 Description Everybody in the Prime Land is using a prime base number system. In this system, each positive integer x is represented as follows: Let {pi}i=0,1,2,

双向广搜 POJ 3126 Prime Path

POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted: 9153 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change th

POJ 3126 Prime Path(素数路径)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom