ruclion的专栏

痴的不是凯旋,而是战斗!

poj 1811

题目描述:

大质数(特别大)的判定质数和分解质数.

题解:

Miller_Rabin 算法进行素数测试 和 pollard_rho 算法进行质因素分解

重点:

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctype.h>
#include <limits.h>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#include <time.h>
#define CLR(a) memset(a, 0, sizeof(a))
#define REP(i, a, b) for(ll i = a;i < b;i++)
#define REP_D(i, a, b) for(ll i = a;i <= b;i++)

typedef long long ll;

using namespace std;

ll n;

const ll S = 8;
ll mul_mod(ll a, ll b, ll c)
{
    a %= c;
    b %= c;
    ll res = 0;
    ll tmp = a;
    while(b)
    {
        if(b&1)
        {
            res = res + tmp;
            if(res >= c)
                res -= c;
        }
        tmp <<= 1;
        if(tmp >= c)
            tmp -= c;
        b >>= 1;
    }
    return res;
}

ll pow_mod(ll x, ll n, ll c)
{
    ll res = 1;
    ll tmp = x%c;
    while(n)
    {
        if(n&1)
        {
            res = mul_mod(res, tmp, c);
        }
        n >>= 1;
        tmp = mul_mod(tmp, tmp, c);
    }
    return res;
}

ll check(ll a, ll n, ll x, ll t)
{
    ll res = pow_mod(a, x, n);
    ll last = res;
    for(ll i=1; i<= t; i++)
    {
        res = mul_mod(res, res, n);
        if(res==1&&(last != 1 && last != n-1))
            return 1;
        last = res;
    }
    if(res!=1)
        return 1;
    return 0;
}

ll miller_rabin(ll n)
{
    if(n < 2)
        return 0;
    if(n==2)
        return 1;
    if((n&1)==0)
        return 0;
    ll x=n-1;
    ll t = 0;
    while((x&1)==0)
    {
        t++;
        x>>=1;
    }
    srand(time(NULL));
    for(ll i=1; i<=S; i++)
    {
        ll a = rand()%(n-1)+1;
        if(check(a, n, x, t))
        {
            return 0;
        }
    }
    return 1;
}


ll gcd(ll a, ll b)
{
    if(a < 0)
        a= -a;
    if(b < 0)
        b = -b;
    //a = abs(a);
    //b = abs(b);
    if(b==0)
        return a;
    return gcd(b, a%b);
}

long long pollard_rho(long long x,long long c)
{
    long long i = 1, k = 2;
    srand(time(NULL));
    long long x0 = rand()%(x-1) + 1;
    long long y = x0;
    while(1)
    {
        i ++;
        x0 = (mul_mod(x0,x0,x) + c)%x;
        long long d = gcd(y - x0,x);
        if( d != 1 && d != x)return d;
        if(y == x0)return x;
        if(i == k)
        {
            y = x0;
            k += k;
        }
    }
}

ll factor[110], tot;
void findfac(ll n, ll k)
{
    if(n==1)
        return;
    if(miller_rabin(n))
    {
        factor[tot++]= n;
        return;
    }
    ll p = n;
    ll c= k;
    while(p >= n)
    {
        p = pollard_rho(p, c);
        c--;
    }
    findfac(p, k);
    findfac(n/p, k);
}

void solve()
{
    if(miller_rabin(n))
    {
        printf("Prime\n");
        return;
    }
    else
    {
        tot= 0;
        findfac(n, 107);
    }
    sort(factor, factor + tot);
    printf("%I64d\n", factor[0]);
}


int main()
{
  //  freopen("2Bin.txt", "r", stdin);
    //freopen("3Bout.txt", "w", stdout);
    ll t;
    scanf("%I64d", &t);
    while(t--)
    {
        scanf("%I64d", &n);
        solve();
    }
    return 0;
}
阅读更多
个人分类: 数论-取模
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

poj 1811

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭