AtCoder Beginner Contest 052

A - Two Rectangles

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int A, B, C, D;
	cin >> A >> B >> C >> D;
	cout << max(A*B, C*D);
	return 0;
}

B - Increment Decrement

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int N, x = 0;
	string S;
	cin >> N >> S;
	int ans = 0;
	for (int i = 0; i < N; i++) {
		if (S[i] == 'I') x++;
		else x--;
		ans = max(ans, x);
	}
	cout << ans;
	return 0;
}

C - Factors of Factorial

前置知识及例题:

约数个数定理:
假设一个数为 \(x\),它可以表示为 \(p_1^{a_1} \times p_2^{a_2} \times ... \times p_k^{a_k}\) ,那么它的约数个数就为 \(\prod\limits_{i=1}^k(a_i+1)\)

例如 \(360\) = \(2^3 \times 3^2 \times 5\),那么 \(2\) 的个数有 \(4\) 种选择(选 \(0\) 个,选 \(1\) 个,选 \(2\) 个,选 \(3\) 个),\(3\) 的个数有 \(3\) 种选择,\(5\) 的个数有 \(2\) 种选择,因为是相互独立的,所以根据乘法原理,得到\(4*3*2=24\)

例题:质因子分解
\(2\)\(n\) 分别求质因子,不需要判断是否为质数,因为每个质因子我们都一直除,直到不能再除,以后也不会出现可以除的数是此数的倍数了,相当于已经筛掉了。

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int N;
	cin >> N;
	vector<int> a(N + 1, 0);
	for (int i = 2; i <= N; i++) {
		int t = i;
		for (int j = 2; j <= i; j++) {
			while (t % j == 0) {
				a[j]++;
				t /= j;
			}
		}
	}
	for (int i = 1; i <= N; i++) {
		if (a[i] != 0) cout << i << " " << a[i] << "\n";//先输出底数再输出指数。
	}
	return 0;
}

这样看本题就很简单了。

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

const int mod = 1e9 + 7;

int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int N;
	cin >> N;
	vector<int> a(N + 1, 0);
	for (int i = 2; i <= N; i++) {
		int t = i;
		for (int j = 2; j <= i; j++) {
			while (t % j == 0) {
				a[j]++;
				t /= j;
			}
		}
	}
	i64 ans = 1;
	for (int i = 2; i <= N; i++) {
		if (a[i] != 0) {
			ans *= (a[i] + 1);
			ans %= mod;
		}
	}
	cout << ans;
  	return 0;
}

D - Walk and Teleport

贪心。 唯一需要注意的是 \(\rm min\) 的两个参数需要类型一致,因此需要开 \(\rm long\) \(\rm long\).

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);
	i64 N, A, B;
	cin >> N >> A >> B;
	vector<int> x(N);
	for (int i = 0; i < N; i++) cin >> x[i];
	i64 ans = 0;
	for (int i = 1; i < N; i++) {
		int d = x[i] - x[i - 1];
		ans += min(d * A, B);
	}
	cout << ans;
	return 0;
}
posted @ 2024-08-27 14:46  胖柚の工作室  阅读(11)  评论(0)    收藏  举报