~~约数个数(附模板题)

模板

如果 N = p1^c1 * p2^c2 * ... *pk^ck
约数个数: (c1 + 1) * (c2 + 1) * ... * (ck + 1)
约数之和: (p1^0 + p1^1 + ... + p1^c1) * ... * (pk^0 + pk^1 + ... + pk^ck)

题目

给定n个正整数ai,请你输出这些数的乘积的约数个数,答案对109+7取模。

输入格式

第一行包含整数n。

接下来n行,每行包含一个整数ai。

输出格式

输出一个整数,表示所给正整数的乘积的约数个数,答案需对109+7取模。

数据范围

1≤n≤100,
1≤ai≤2∗109

输入样例:

3
2
6
8

输出样例:

12

#include <bits/stdc++.h>
using namespace std;
typedef long long LL; 
const int mod = 1e9 + 7;
int main(){
    int n,x;
    LL ans = 1;
    unordered_map<int,int> hash;
    cin >> n;
    while(n--){
        cin >> x;
        for(int i = 2;i <= x/i; ++i){
            while(x % i == 0){
                x /= i;
                hash[i] ++;
            }
        }
        if(x > 1) hash[x] ++;
    }
    for(auto i : hash) ans = ans*(i.second + 1) % mod;
    cout << ans;
    return 0;
}
发布了164 篇原创文章 · 获赞 440 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_45884316/article/details/105220622