[AcWing 871] 约数之和

image

复杂度 $ O(\sqrt{n}) $

总体复杂度 $ 100 \times \sqrt{2 \times 10^{9}} \approx 4.5 \times 10^{6} $


点击查看代码
#include<iostream>
#include<unordered_map>

using namespace std;
typedef long long LL;
const int N = 110, mod = 1e9 + 7;
unordered_map<int, int> primes;

void solve(int x)
{
    for (int i = 2; i <= x / i; i ++) {
        while (x % i == 0) {
            x /= i;
            primes[i] ++;
        }
    }
    if (x > 1)  primes[x] ++;
}
int main()
{
    int n;
    cin >> n;
    while (n --) {
        int x;
        cin >> x;
        solve(x);
    }
    LL res = 1;
    for (auto prime : primes) {
        int p = prime.first, a = prime.second;
        LL t = 1;
        while (a --)    t = (t * p + 1) % mod;
        res = res * t % mod;
    }
    cout << res << endl;
    return 0;
}

  1. 约数之和的公式为:(如果把式子展开,每一个乘积项都对应一个约数)
    $M=(P_1^0 + P_1^1 + \cdots + P_1^{\alpha_1}) \cdots (P_n^0 + P_n^1 + \cdots + P_n^{\alpha_n}) $
  2. \(P_1^0 + P_1^1 + \cdots + P_1^{\alpha_1}\) 的操作:LL t = 1,每次让 $ t = ( t * p ) + 1 $,一开始 $ t = 1$,计算一次 \(t = p + 1\),计算两次 \(t = p^2 + p + 1\),那么计算 \(\alpha\) 次过后,\(t = p^0 + p^1 + \cdots + p^{\alpha}\)
posted @ 2022-05-08 21:42  wKingYu  阅读(83)  评论(0)    收藏  举报