HDU 4344 Mark the Rope Pollard_Rho大数分解

本文详细介绍了一种高效的质因数分解算法,并提供了完整的C++实现代码。该算法结合了Miller-Rabin素性测试和Pollard Rho算法,能够有效地找到大整数的所有质因数。文中还解释了一些关键函数,如快速幂模运算等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PS:图片由作者用wps制作,使用请注明链接,O(∩_∩)O谢谢!

注意如果n的质因数分解只用到一个质数时,要多除一个质数。

如对Miller-Rabin有疑问,请参考作者其他博客:Miller-Rabin素性测试算法详解

代码如下:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>

using namespace std;

typedef long long int ll;

const int MAX_SIZE = 3000;
ll fac[MAX_SIZE], ct;
ll num[MAX_SIZE], cnt;

ll GCD(ll x, ll y)
{
    return !y ? x : GCD(y, x % y);
}

typedef long long int ll;

ll mod_mul(ll a, ll b, ll mod)
{
    ll res = 0;
    while (b)
    {
        if (b & 1)
            res = (res + a) % mod;
        a = (a + a) % mod;
        b >>= 1;
    }
    return res;
}

ll mod_pow(ll a, ll n, ll mod)
{
    ll res = 1;
    while (n)
    {
        if (n & 1)
            res = mod_mul(res, a, mod);
        a = mod_mul(a, a, mod);
        n >>= 1;
    }
    return res;
}

bool Miller_Rabin(ll n)
{
    if (n == 2)
        return true;
    if (n < 2 || !(n & 1))
        return false;
    ll m = n - 1, k = 0;
    while (!(m & 1))
    {
        k++;
        m >>= 1;
    }
    for (int i = 1; i <= 20; i++)
    {
        ll a = rand() % (n - 1) + 1;
        ll x = mod_pow(a, m, n);
        ll y;
        for (int j = 1; j <= k; j++)
        {
            y = mod_mul(x, x, n);
            if (y == 1 && x != 1 && x != n - 1)
                return false;
            x = y;
        }
        if (y != 1)
            return false;
    }
    return true;
}

ll Pollard_Rho(ll n, ll c)
{
    ll i = 1, k = 2;
    ll x = rand() % (n - 1) + 1, y = x;
    while (1)
    {
        i++;
        x = (mod_mul(x, x, n) + c) % n;
        ll d = GCD((y - x + n) % n, n);
        if (d != 1 && d != n)
            return d;
        if (y == x)
            return n;
        if (i == k)
        {
            y = x;
            k <<= 1;
        }
    }
}

void find(ll n, ll c)
{
    if (n == 1)
        return;
    if (Miller_Rabin(n))
    {
        fac[ct++] = n;
        return;
    }
    ll p = n;
    ll k = c;
    while (p >= n)
        p = Pollard_Rho(p, c--);
    find(p, k);
    find(n / p, k);
}

int main()
{
    //freopen("test.txt", "r", stdin);

    int T;
    scanf("%d", &T);
    while (T--)
    {
        ll N;
        scanf("%I64d", &N);
        ct = 0;
        find(N, 113);
        sort(fac, fac + ct);
        memset(num, 0, sizeof(num));
        cnt = 0;
        num[0] = 1;
        ll ans = 0, t = fac[0];
        for (int i = 1; i < ct; i++)
        {
            if (fac[i] == fac[i - 1])
            {
                t *= fac[i];
                num[cnt]++;
            }
            else
            {
                ans += t;
                t = fac[i];
                fac[++cnt] = fac[i];
                num[cnt] = 1;
            }
        }
        ans += t;
        if (cnt == 0)
            ans /= fac[0];
        printf("%I64d %I64d\n", cnt + 1, ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值