【AcWing】871. 约数之和

分解完质因数后,直接代入求和公式。

cpp 复制代码
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;

typedef long long LL;

const int 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]++;//大于sqrt(x)的质因数
    }
    
    LL res=1;
    for(auto prime:primes){
        int p=prime.first,a=prime.second;//p是底数,a是指数
        LL t=1;
        while(a--){//乘指数次
            t=(t*p+1)%mod;//求每一个p的和
        }
        res=res*t%mod;//相乘
    }
    cout<<res<<endl;
    return 0;
}
相关推荐
还有多久拿退休金7 小时前
让飞书知识库跟着 commit 自己长:一套自动化知识库的真实实现
前端·算法·架构
KaMeidebaby8 小时前
卡梅德生物技术快报|小 RNA 适配体合成 + 多方法亲和力表征全流程标准化操作手册
前端·网络·数据库·人工智能·算法
是Dream呀8 小时前
基于深度学习的人类行为识别算法研究
人工智能·深度学习·算法
happyprince9 小时前
03_NVIDIA_ModelOpt-量化算法深入
人工智能·深度学习·算法
大鱼>9 小时前
AI+货物追踪:智能快递柜追踪系统
人工智能·深度学习·算法·机器学习
researcher-Jiang9 小时前
算法训练:堆 & 可并堆
算法
郝学胜-神的一滴9 小时前
中级OpenGL教程 013:渲染器类架构设计与逐帧渲染流程详解
开发语言·c++·unity·游戏引擎·图形渲染·opengl·unreal
在书中成长10 小时前
HarmonyOS 小游戏《对战五子棋》开发第18篇 - 棋盘设计
算法·harmonyos
Frostnova丶10 小时前
(12)LeetCode 76. 最小覆盖子串
算法·leetcode·职场和发展
灯澜忆梦10 小时前
GO_函数_1
算法