【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;
}
相关推荐
h397417 分钟前
DirectShow过滤器开发-视频源过滤器
c++·windows·音视频·directshow
程序猿编码2 小时前
榨干 RTX5090 算力!Qwen3 专用单卡推理引擎,手写C++/CUDA 算子实现 MTP 推测解码
开发语言·c++·大模型推理·qwen3·多模态推理
西飘客3 小时前
vs 怎么根据dll 生成lib文件
c++·qt
程序员老陆3 小时前
深入理解 C++ thread_local:线程私有存储的正确打开方式
开发语言·c++·程序设计
信奥卷王4 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵4 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
徐小黑ACG5 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
码完就睡6 小时前
数据结构——红黑树
数据结构
码爸6 小时前
数据结构详细介绍
数据结构