Codeforces Round #691 (Div. 2)

A

题目链接:

https://codeforces.com/contest/1459/problem/A

解题思路:
从本质上来讲,这是一道概率题。对应位置上,数字的权重是一样的,所以如果对应位置上前者比后者大,那么对于所有的全排列情况来说,前者大的情况的比例会加上1。最后只需要比较,哪个数对应位置上数多就行了。
#include <bits/stdc++.h>
using namespace std;

int main () {
	int T;
	cin >> T;
	while(T--) {
		int n;
		cin >> n;
		string t1, t2;
		cin >> t1 >> t2;
		int num1;
		int num2;
		num1 = num2 = 0;
		for(int i = 0; i < n; ++i) {
			if(t1[i] > t2[i]) {
				num1++;
			} 
			if(t1[i] < t2[i]) {
				num2++;
			}
		}
		if(num1 > num2) {
			cout << "RED" << endl;
		} else if(num1 < num2) {
			cout << "BLUE" << endl;
		} else {
			cout << "EQUAL" << endl;
		}
	}
}

B

题目链接:

https://codeforces.com/contest/1459/problem/B

解题思路:

这是一道找规律的题,最后找到了一个数学家推出的公式。。。

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
int main () {
	ll n;
	cin >> n;
	ll ans = 1 + (3 * n * (n + 4) + 2 - (n % 2 ? - 1 : 1) * (n * (n + 4) + 2)) / 8;
	cout << ans << endl;
}

当然,这道题也可以用找规律的方法。

下图是n为1和2的情况。

n为3

n为4

n为5

由数学猜想,我们可以猜出如果是偶数,结果一定为某个数的平方。

然后奇数的时候,结果为上个奇数的情况加上4乘以某个数。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAXN = 1e5 + 10;
ll ans[MAXN];
int main (){
	ans[1] = ans[2] = 4;
	int n;
	cin >> n;
	ll tot = 2;
	for(int i = 3; i <= n; ++i) {
		if(i & 1) {
			ans[i] = ans[i - 2] + tot * 4; 
		} else {
			tot++;
			ans[i] = tot * tot;
		}
	}
	cout << ans[n] << endl;
}

C

题目链接:

https://codeforces.com/contest/1459/problem/C

解题思路:
公式题:gcd(a, b1, b2...) = gcd(a, b1- a, b2 - a, ....) 。注意排序。
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
int main () {
	std::vector<ll> a;
	ll n, m;
	cin >> n >> m;
	a.resize(n);
	for(int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	sort(a.begin(), a.end());
	for(int i = 1; i < n; ++i) {
		a[i] -= a[0];
	}
	ll num = 0ll;
	for(int i = n - 1; i > 0; --i) {
		num = __gcd(num, a[i]);
	}
	std::vector<ll> b;
	for(int i = 0; i < m; ++i) {
		ll temp;
		cin >> temp;
		b.push_back(__gcd(num, a[0] + temp));
	}
	for(int i = 0; i < m; ++i) {
		cout << b[i] << " ";
	}
	cout << endl;
}
原文地址:https://www.cnblogs.com/lightac/p/14162759.html