【Algorithm】约数

试除法求所有约数

vector<int> get_divisors(int x)
{
    vector<int> res;
    for (int i = 1; i <= x / i; i ++ )
        if (x % i == 0)
        {
            res.push_back(i);
            if (i != x / i) res.push_back(x / i);
        }
    sort(res.begin(), res.end());
    return res;
}

约数个数和约数之和

正整数 N 能够被唯一分解为 N = p_1^{c_1} * p_2^{c_2} * ... * p_k^{c_k}
约数个数:(c_1 + 1) * (c_2 + 1) * ... * (c_k+1)
约数之和: (p_1^0 + p_1^1 + p_1^2 + ... + p_1^{c_1}) * ... * (p_k^0 + p_k^1 + p_k^2 + ... + p_k^{c_k})

#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long LL;

const int N = 110, mod = 1e9 + 7;

int main()
{
    int n;
    cin >> n;

    unordered_map<int, int> primes;

    while (n -- )
    {
        int x;
        cin >> x;

        for (int i = 2; i <= x / i; i ++ )
            while (x % i == 0)
            {
                x /= i;
                primes[i] ++ ;
            }

        if (x > 1) primes[x] ++ ;
    }

    LL res = 1;
    for (auto p : primes) res = res * (p.second + 1) % mod;

    cout << res << endl;

    return 0;
}

#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long LL;

const int N = 110, mod = 1e9 + 7;

int main()
{
    int n;
    cin >> n;

    unordered_map<int, int> primes;

    while (n -- )
    {
        int x;
        cin >> x;

        for (int i = 2; i <= x / i; i ++ )
            while (x % i == 0)
            {
                x /= i;
                primes[i] ++ ;
            }

        if (x > 1) primes[x] ++ ;
    }

    LL res = 1;
    for (auto p : primes)
    {
        LL a = p.first, b = p.second;
        LL t = 1;
        while (b -- ) t = (t * a + 1) % mod;
        res = res * t % mod;
    }

    cout << res << endl;

    return 0;
}

约数之和: (p_1^0 + p_1^1 + p_1^2 + ... + p_1^{B * c_1}) * ... * (p_k^0 + p_k^1 + p_k^2 + ... + p_k^{B * c_k})

k 较小,但是 B 是5e7,遍历等比数组求和会超时。

可以分治来做:

例如,求 p^0 + p^1 + p^2 + ... + p^{k - 1}  即 sum(p, k)

通用求等比数列和的方法

  1. 如果 k 为偶数,sum(p, k) = p^0 + p^1 + ... + p^{(k/2 - 1)} + p^{(k/2)} + p^{(k/2+1)} + ... + p^{k - 1} = p^0 + p^1 + ... + p^{(k/2 - 1)} + p^{(k/2)} * (p^0 + p^1 + ... + p^{(k/2 - 1)} ) = (1 + p^{k/2}) * sum(p, k / 2)
  2. 如果 k 为奇数,sum(p, k) = p^0 + p^1 + ... + p^{k - 1} = p^0 + p * (p^0 + p^1 + ... + p^{k-2}) = 1 + p * sum(p, k - 1); 或者 sum(p, k - 1) + p^{k-1}
#include <cstdio>

const int mod = 9901;

int qmi(int a, int k)
{
    int res = 1;
    a %= mod;
    while (k)
    {
        if (k & 1) res = res * a % mod;
        a = a * a % mod;
        k >>= 1;
    }
    return res;
}

int sum(int p, int k)
{
    if (k == 1) return 1;
    if (k % 2 == 0) return (1 + qmi(p, k / 2)) * sum(p, k / 2) % mod;
    return (sum(p, k - 1) + qmi(p, k - 1)) % mod;
}

int main()
{
    int a, b;
    scanf("%d%d", &a, &b);

    int res = 1;
    // 对a分解质因数
    for (int i = 2; i * i <= a; i ++ )
        if (a % i == 0)
        {
            int s = 0;
            while (a % i == 0)
            {
                a /= i, s ++ ;
            }
            res = res * sum(i, b * s + 1) % mod;
        }

    if (a > 1) res = res * sum(a, b + 1) % mod;
    if (a == 0) res = 0;

    printf("%d\n", res);

    return 0;
}

欧几里得算法 -- 最大公约数

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值