【codeforces之路】(简单的数学题)(1438字)(不定期更新)

目录

第一题:A. Divide and Conquer

第二题:B. Make Array Good

第三题:A. Extremely Round


 

时间2022.12.16,rating:1186。

第一题:A. Divide and Conquer

求使得数列和为偶数的最小操作次数,如果原来是偶数的话为0,如果是奇数的话,只需要找到最小的改变奇偶性的次数,由于数比较小可以直接暴力枚举解决。

实现:

#include 
#define ll long long
using namespace std;
const int N = 55;
int a[N];
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		int sum = 0;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			sum += a[i];
		}
		if (sum % 2 == 0) {
			cout << 0 << '\n';
		} else {
			int ans = 0x3f3f3f3f;
			for (int i = 0; i < n; i++) {
				int cnt = 0;
				if (a[i] & 1) {
					while (a[i] & 1) {
						a[i] >>= 1;
						cnt++;
					}
				} else {
					while (a[i] % 2 == 0) {
						a[i] >>= 1;
						cnt++;
					}
				}
				ans = min(ans, cnt);
			}
			cout << ans << '\n';
		}
	}
	return 0;
}

第二题:B. Make Array Good

这道题的目的是构造出一个数列,使得任取两个数其中较小的可以整除较大的,我们可以想到构造出一个2的等比数列,显然高次的可以被低次的整除。

对于任意的一个数可以写成1010010101010101的二进制数乘2 ->1010010101010101 + 0,两者之间必然闭区间,必然有1000000000000000 + 0 <= 1010010101010101 + 0 可以到达。

于是实现:

#include 
#include 
#define ll long long
using namespace std;
const int N = 1e5 + 5;
ll a[N];
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
		}
		cout << n << '\n';
		for (int i = 1; i <= n; i++) {
			cout << i << ' ' << (1 << __lg(a[i]) + 1) - a[i] << '\n';
		} 
	}
	return 0;
}

第三题:A. Extremely Round

找规律,1是特殊的,10有9个这样的数,100有9个这样的数,得实现。

#include 
#define ll long long
#define inf 0x3f3f3f3f
//const int N = 
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		if (n <= 10) {
			cout << n << '\n';
			continue;
		}
		int cnt = 0;
		while (n >= 10) {
			n /= 10;
			cnt++;
		}
		cout << cnt * 9 + n << '\n';
	}
	return 0;
}

你可能感兴趣的:(算法,c++,开发语言)