acwing \870. 约数个数
题目描述
给定 nn 个正整数 aiai,请你输出这些数的乘积的约数个数,答案对 109+7 取模。
输入格式
第一行包含整数 n。
接下来 n 行,每行包含一个整数 ai。
输出格式
输出一个整数,表示所给正整数的乘积的约数个数,答案需对 109+7取模。
数据范围
1≤n≤100
1≤ai≤2×109输入样例:
3 2 6 8
输出样例:
12
试除法求质因数
分析
代码
#include
#include
#include
using namespace std;
typedef long long ll; // 注意最后的结果要用long long类型
const int mod = 1e9 + 7;
int n;
unordered_map h; // 统计每个质数在总的乘积中出现了几次
int main()
{
// cout << h[1] << " hhh " << endl;
scanf("%d", &n);
while(n --)
{
int a;
scanf("%d", &a);
for(int i = 2; i <= a/i; i++)
{
if(a % i == 0) // 找到一个因子(质因子)
{
int cnt = 0;
while(a % i == 0) // 循环除这个质因子
{
cnt++;
a /= i;
}
h[i] += cnt; // 统计所有质因数的个数
}
}
// 最后一个质因子
if(a > 1) h[a]++;
}
// for(auto t : h) printf("%d %d\n", t.first, t.second );
// return 0;
ll res = 1;
for(auto item : h) res = (res%mod * (item.second + 1)%mod) % mod;
printf("%d\n", res);
return 0;
}
时间复杂度
参考文章
https://www.acwing.com/solution/content/4969/