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

news/2024/7/7 20:30:41

目录

  • 1 基础知识
  • 2 模板
  • 3 工程化

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;
}

http://lihuaxi.xjx100.cn/news/1787249.html

相关文章

《深入浅出进阶篇》——空间换时间优化——P2671 求和

链接&#xff1a;https://www.luogu.com.cn/problem/P2671 上题干&#xff1a; 题目描述 一条狭长的纸带被均匀划分出了n个格子&#xff0c;格子编号从11到n。每个格子上都染了一种颜色colori​用[1,m]当中的一个整数表示&#xff09;&#xff0c;并且写了一个数字numberi​。…

vue中如何将json数组指定的key赋值给el-form-item并均匀的分成2列

在Vue中&#xff0c;你可以使用v-for指令来遍历JSON数组&#xff0c;并将指定的key赋值给el-form-item。下面是一个示例&#xff1a; <template><el-form><el-row><el-col :span"6" v-for"item in jsonArray" :key"item.key&qu…

[运维工具]ubuntu下迁移home目录至新的分区教程详解

ubuntu下迁移home目录至新的分区教程详解 前言 首先声明一下&#xff0c;因为此教程涉及到用户重要资料数据&#xff0c;所以操作前&#xff1a; 数据无价&#xff0c;请一定要先备份&#xff01;数据无价&#xff0c;请一定要先备份&#xff01;数据无价&#xff0c;请一定…

JAVA数据代码示例

首先&#xff0c;我们需要导入一些必要的Java库 java import java.net.URL; import java.net.HttpURLConnection; import java.io.BufferedReader; import java.io.InputStreamReader; 然后&#xff0c;我们可以创建一个URL对象&#xff0c;表示我们要爬取的网页的URL。 jav…

智慧城市怎么实时监测内涝积水的发生及解决办法?

随着城市化进程步伐不断加快&#xff0c;城市内涝问题越来越受到人们的关注。内涝不仅不便于人们的生活&#xff0c;还可能危害城市之中的基础设施比如路面等。因此实时监测内涝积水的发生并采取有效的解决办法是市政府的紧急任务&#xff0c;同时解决城市内涝也利于城市生命线…

解决Dockerfile中 Could not initialize class sun.awt.X11FontManager错误

Dockerfile中增加命令 RUN yum install dejavu-sans-fonts fontconfig -y如果您使用的是基于Alpine Linux的发行版&#xff0c;可以使用apk命令来安装DejaVu Sans字体和fontconfig工具 RUN apk update RUN apk add ttf-dejavu fontconfig

Windows系统下使用docker部署redis

使用虚拟机部署redis&#xff0c;虚拟机很占用电脑资源&#xff0c;所以选择使用docker对redis进行部署。 一、安装docker 安装链接&#xff1a;https://docker.p2hp.com/ 二、配置redis.conf文件 下载配置文件&#xff1a;https://download.redis.io/redis-stable/redis.con…

高教社杯数模竞赛特辑论文篇-2023年A题:定日镜场的输出功率优化(附获奖论文及MATLAB代码实现)(中)

目录 6.4定日镜平均输出热功率优化模型的求解 6.5问题二求解结果 6.6 结果分析