[AcWing 871] 约数之和

image

复杂度 O(n)

总体复杂度 100×2×1094.5×106


点击查看代码
#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=(P10+P11++P1α1)(Pn0+Pn1++Pnαn)
  2. P10+P11++P1α1 的操作:LL t = 1,每次让 t=(tp)+1,一开始 t=1,计算一次 t=p+1,计算两次 t=p2+p+1,那么计算 α 次过后,t=p0+p1++pα
posted @   wKingYu  阅读(55)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
欢迎阅读『[AcWing 871] 约数之和』
点击右上角即可分享
微信分享提示