【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;
}
相关推荐
阿Y加油吧11 小时前
算法实战笔记:LeetCode 169 多数元素 & 75 颜色分类
笔记·算法·leetcode
t***54411 小时前
如何在Dev-C++中选择Clang编译器
开发语言·c++
不要秃头的小孩11 小时前
力扣刷题——509. 斐波那契数
python·算法·leetcode·动态规划
汉克老师11 小时前
GESP2023年9月认证C++三级( 第一部分选择题(9-15))
c++·gesp三级·gesp3级
We་ct12 小时前
LeetCode 120. 三角形最小路径和:动态规划详解
前端·javascript·算法·leetcode·typescript·动态规划
py有趣12 小时前
力扣热门100题之和为K的子数组
数据结构·算法·leetcode
hipolymers12 小时前
C语言怎么样?难学吗?
c语言·数据结构·学习·算法·编程
CS创新实验室13 小时前
从“跑得动”到“跑得稳”:深度剖析数据结构究竟是理论点缀还是核心战力?
数据结构
jllllyuz14 小时前
MATLAB 蒙特卡洛排队等待模拟程序
数据结构·matlab