数学知识:约数

试除法求约数

题解
Y总

给定 n 个正整数 ai,对于每个整数 ai,
请你按照从小到大的顺序输出它的所有约数。

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

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

输出格式
输出共 n 行,其中第 i 行输出第 i 个整数 ai 的所有约数。

数据范围
1 ≤ n ≤ 100,
2 ≤ ai ≤ 2 × 109

输入样例:
2
6
8

输出样例:
1 2 3 6 
1 2 4 8
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> get_divisort(int x) 
{
	vector<int> res;
	for (int i = 1; i <= x / i; i ++ ) {
		if (x % i == 0) {
			res.push_back(i);
			if (i != x / i) res.push_back(x / i);
		}
	}
	sort(res.begin(), res.end());
	return res; 
}
int main()
{
	int n;
	cin >> n;
	while (n -- ) {
		int x;
		cin >> x;
		auto num = get_divisort(x);
		for(auto res : num) cout << res << " ";
		cout << endl; 
	}
	return 0; 
} 

约数个数

题解
Y总

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

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

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

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

数据范围
1 ≤ n ≤ 100,
1 ≤ ai ≤ 2 × 109

输入样例:
3
2
6
8

输出样例:
12
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long LL;
const int N = 110, mod = 1e9 + 7; 
int main()
{
	int n;
	cin >> n;
	unordered_map<int, int> primes;
	while(n -- ) {
		int x;
		cin >> x;
		for (int i = 2; i <= x / i; i ++ ) 
			while (x % i == 0) {
				x /= i;
				primes[i] ++ ;
			}
		if (x > 1) primes[x] ++ ;	
	}
	LL res = 1;
	for (auto p : primes) res = res * (p.second + 1) % mod;
	cout << res << endl;
	return 0;
} 

约数之和

题解
Y总

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

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

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

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

数据范围
1 ≤ n ≤ 100,
1 ≤ ai ≤ 2 × 109

输入样例:
3
2
6
8

输出样例:
252
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 110, mod = 1e9 + 7;
int main()
{
	int n;
	cin >> n;
	unordered_map<int, int> primes;
	while (n -- ) {
		int x;
		cin >> x;
		for (int i = 2; i <= x / i; i ++ ) 
			while (x % i == 0) {
				x /= i;
				primes[i] ++ ;
			}
		if (x > 1) primes[x] ++ ;
	}
	LL res = 1;
	for (auto p : primes) {
		LL a = p.first, b = p.second;
		LL t = 1;
		while (b -- ) t = (t * a + 1) % mod;
		res = res * t % mod;
	}
	cout << res << endl;
	return 0;
}

最大公约数

题解
Y总

给定 n 对正整数 ai,bi,请你求出每对数的最大公约数。

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

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

输出格式
输出共 n 行,每行输出一个整数对的最大公约数。

数据范围
1 ≤ n ≤ 105,
1 ≤ ai,bi ≤ 2 × 109

输入样例:
2
3 6
4 6

输出样例:
3
2
#include <iostream>
#include <algorithm>
using namespace std;
int gcd(int a, int b) 
{
	return b ? gcd(b, a % b) : a;
}
int main()
{
	int n;
	cin >> n;
	while (n -- ) {
		int a, b;
		cin >> a >> b;
		cout << gcd(a, b) << endl; 
	}
	return 0;	
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值