【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;
}
相关推荐
小帽子_1236 小时前
大功率 PCS 双环闭环控制算法原理与实现
算法
buhuizhiyuci7 小时前
【算法篇】位运算 —— 基础篇
java·数据库·算法
Black蜡笔小新7 小时前
算法训练完了怎么上线?DLTM→AIS→EasyGBS三步交付流水线
人工智能·算法
孬甭_7 小时前
C++ string类
开发语言·c++
sycmancia7 小时前
Qt——多线程与界面组件的通信
开发语言·qt·算法
想吃火锅10057 小时前
【leetcode】5.最长回文子串js
算法·leetcode·职场和发展
盐焗鹌鹑蛋8 小时前
【C++】C++11:右值引用、移动语义万能引用
c++
LingzhiPi8 小时前
零知ESP32--RC522NFC考勤打卡系统
c++·单片机·嵌入式硬件
CoderYanger8 小时前
A.每日一题:1967题+1331题
java·程序人生·算法·leetcode·面试·职场和发展·学习方法
Jasmine_llq8 小时前
《P17010 [GESP202606 五级] 排排坐》
算法·贪心算法·数学化简求和式·排序sort 反向迭代器降序·线性遍历累加计算总权重和