【基础算法】约数个数&约数之和(Acwing870题&Acwing871题)

 关于约数(划重点)

如果 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)

 关于取模

(a + b) % p = (a%p + b%p) %p 
(a - b) % p = (a%p - b%p) %p 
(a * b) % p = (a%p * b%p) %p 

870.题目

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

输入格式
第一行包含整数n。

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

输出格式
输出一个整数,表示所给正整数的乘积的约数个数,答案需对10^{9}+7取模。

数据范围
1≤n≤100,
1≤ai≤2∗10^{9}
输入样例

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;
    unodered_map<int,int>hash;
    cin>>n;
    while(n--)
    {
        cin>>x;
        for(int i=2;i<=x;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;
}

(题目分割线~)

871.题目 

给定n个正整数ai,请你输出这些数的乘积的约数之和,答案对10^{9}+7取模。 

输入格式
第一行包含整数n。

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

输出格式
输出一个整数,表示所给正整数的乘积的约数之和,答案需对10^{9}+7取模。 

数据范围
1≤n≤100,
1≤ai≤2∗10^{9}
输入样例

3
2
6
8


输出样例

252

代码

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const int mod=1e9+7;

int main()
{
	int n,x;
	LL res=1;
	unordered_map<int,int>hash;
	cin>>n;
	while(n--)
	{
		cin>>x;
		for(int i=2;i<=x;i++)
		{
			while(x%i==0)
			{
				x/=i;
				hash[i]++;
			}
		}
		if(x>1) hash[x]++;
	} 
	for(auto i:hash)
	{
		int a=i.first,b=i.second;
		int t=1;
		while(b--) t=(1+a*t)%mod;
		res=res*t%mod;
	}
	cout<<res<<endl;
	return 0;
} 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值