acwing算法基础之数学知识--求一个数x的约数数目和约数之和

123 篇文章 1 订阅

1 基础知识

求一个数x的约数数目和约数之和的关键步骤:

  1. 对数x分解质约数,
    x = p 1 c 1 ⋅ p 2 c 2 ⋯ p k c k x=p_1^{c_1}\cdot p_2^{c_2}\cdots p_k^{c_k} x=p1c1p2c2pkck
unordered_map<int,int> get_prime_divisors(int x) {//对一个数x进行分解质因子操作
	unordered_map<int,int> mp;
	for (int i = 2; i <= x / i; ++i) {
		if (x % i == 0) {
			int s = 0;
			while (x % i == 0) {
				x /= i;
				s++;
			}
			mp[i] = s;
		}
	}
	if (x > 1) mp[x] = 1;
	return mp;
}
  1. 那么约数总数计算如下,
    c n t = ( c 1 + 1 ) ( c 2 + 1 ) ⋯ ( c k + 1 ) cnt=(c_1+1)(c_2+1)\cdots(c_k+1) cnt=(c1+1)(c2+1)(ck+1)
  2. 那么约数之和计算如下,
    s = ( p 1 0 + p 1 1 + ⋯ + p 1 c 1 ) ( p 2 0 + p 2 1 + ⋯ + p 2 c 2 ) ⋯ ( p k 0 + p k 1 + ⋯ + p k c k ) s=(p_1^0+p_1^1+\cdots+p_1^{c_1})(p_2^0+p_2^1+\cdots + p_2^{c_2})\cdots(p_k^0+p_k^1+\cdots+p_k^{c_k}) s=(p10+p11++p1c1)(p20+p21++p2c2)(pk0+pk1++pkck)

2 模板

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

3 工程化

题目1:求一串数的乘积的约数总数。

#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

const int mod = 1e9 + 7;
unordered_map<int,int> mp;

void get_prime_divisors(int x) {
    //cout << "x = " << x << endl;
    for (int i = 2; i <= x / i; ++i) {
        if (x % i == 0) {
            int s = 0;
            while (x % i == 0) {
                x /= i;
                s++;
            }
            mp[i] += s;
            //cout << "i = " << i << ", s = " << s << endl;
        }
    }
    if (x > 1) {
        mp[x] += 1;
        //cout << "x = " << x << ", mp[x] = " << mp[x] << endl;
    }
    return;
}

int main() {
    int n;
    cin >> n;
    while (n--) {
        int x;
        cin >> x;
        get_prime_divisors(x);
    }
    
    long long res = 1;
    for (auto [x, y] : mp) {
        res *= (y + 1);
        res %= mod;
    }
    cout << res << endl;
    
    return 0;
}

题目2:求一串数的约数之和。

#include <iostream>
#include <unordered_map>

using namespace std;

const int mod = 1e9 + 7;
unordered_map<int,int> mp;

void get_prime_divisors(int x) {
    for (int i = 2; i <= x / i; ++i) {
        if (x % i == 0) {
            int s = 0;
            while (x % i == 0) {
                x /= i;
                s++;
            }
            mp[i] += s;
        }
    }
    if (x > 1) mp[x] += 1;
    return;
}

int main() {
    int n;
    cin >> n;
    while (n--) {
        int x;
        cin >> x;
        
        get_prime_divisors(x);
    }
    
    long long res = 1;
    for (auto [x, a] : mp) {
        //遍历每一个质数x
        //cout << "x = " << x << ", a = " << a << endl;
        long long t = 1;
        while (a--) {
            t = (t * x + 1) % mod;
        }
        res = (res * t) % mod;
    }
    cout << res << endl;
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YMWM_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值