算法基础-数学知识-约数

NO1.试除法求约数

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

输入格式

第一行包含整数 n。

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

输出格式

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

数据范围

1≤n≤100,
1≤ai≤2×10^9

输入样例:
2
6
8
输出样例:
1 2 3 6 
1 2 4 8 

 代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<unordered_map>
#define int long long
using namespace std;
typedef pair<int,int> PII;
const int N = 1e5 + 10;
vector<int> get_divisors(int x){
	vector<int> divisor;
	for(int i = 1;i <= x / i;i ++){
		if(x % i == 0){
			divisor.push_back(i);
			if(i != x / i){
				divisor.push_back(x / i);
			}
		}
	}
	sort(divisor.begin(),divisor.end());
	return divisor;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	while(n --){
		int x;
		cin >> x;
		auto ans = get_divisors(x);
		for(auto t : ans){
			cout << t << ' ';
		}
		cout << endl;
	}
	return 0;
}

NO2.约数个数

给定 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<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<unordered_map>
#define int long long
using namespace std;
typedef pair<int,int> PII;
const int mod = 1e9 + 7;

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	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){
				primes[i] ++;
				x /= i;
			}
		}
		if(x > 1) primes[x] ++;
	}
	long long res = 1;
	for(auto t : primes){
		res = res * (t.second + 1) % mod;
	}
	cout << res << endl;
	return 0;
}

NO3.约数之和

给定 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<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<unordered_map>
#define int long long
using namespace std;
typedef pair<int,int> PII;
const int mod = 1e9 + 7;

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	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){
				primes[i] ++;
				x /= i;
			}
		}
		if(x > 1) primes[x] ++;
	}
	long long res = 1;
	for(auto t : primes){
		int di = t.first,zhi = t.second;
		int ans = 1;
		while(zhi --){
			ans = (ans * di + 1) % mod;
		}
		res = ans * res % mod;
	}
	cout << res << endl;
	return 0;
}

 NO3.最大公约数

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

输入格式

第一行包含整数 n。

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

输出格式

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

数据范围
1≤n≤10^5,
1≤ai,bi≤2×10^9
输入样例:
2
3 6
4 6
输出样例:
3
2

 代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<unordered_map>
#define int long long
using namespace std;
typedef pair<int,int> PII;
const int N = 1e5 + 10;
int gcd(int a,int b){
	return b ? gcd(b,a%b) : a;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	while(n --){
		int a,b;
		cin >> a >> b;
		cout << gcd(a,b) << endl;
	}
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值