Codeforces Round #691 (Div. 2) A~C
A.
签到,因为是等概率,且红色数字两两不同,黑色数字两两不同,故只需要看第一张即可。然后就是枚举每张卡片比较上面的两个数字。
#include <iostream>
using namespace std;
int main()
{
freopen("data.txt", "r", stdin);
int t;
cin >> t;
while(t--)
{
int red = 0, blue = 0, equal = 0;
string s1, s2;
int n;
cin >> n;
cin >> s1;
cin >> s2;
for(int i = 0; i < n; i++)
{
if(s1[i] > s2[i]) red++;
else if(s1[i] == s2[i]) equal++;
else blue++;
}
if(red > blue) cout << "RED" << endl;
else if(red == blue) cout << "EQUAL" << endl;
else cout << "BLUE" << endl;
}
return 0;
}
B.
打表找规律。。。
#include <iostream>
using namespace std;
int main()
{
freopen("data.txt", "r", stdin);
int n;
cin >> n;
if(n & 1)
{
int nn = (n + 1) / 2;
cout << 4 + (nn - 1) * 8 + 2 * (nn - 1) * (nn - 2);
}
else
{
int nn = n / 2 + 1;
cout << nn * nn;
}
}
C.
不妨令b > a,注意到,\(gcd(a, b) = gcd(a, b - a)\),故有\(gcd(a + c, b + c) = gcd(a + c, b - a)\)。又因为\(gcd(a, b, c) = gcd(a, gcd(b, c))\), 故有\(gcd(a_0 + b, a_1+b,a_2+b) = gcd(a_0+b, a_1+b, a_2-a_1)=gcd(a_0+b,a_1-a_0,a_2-a_1)\)...
因此我们可以先把\(GCD=gcd(a_1-a_0,a_2-a_1,...,a_n-a_{n - 1})\)求出来,然后遍历b数组求出\(gcd(a_0+b_i,GCD)\)输出即可。
#include <iostream>
using namespace std;
int n, m;
long long a[200005], b[200005];
long long gcd(long long a, long long b)
{
return b ? gcd(b, a % b) : a;
}
int main()
{
freopen("data.txt", "r", stdin);
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= m; i++) cin >> b[i];
sort(a + 1, a + n + 1);
long long GCD = a[2] - a[1];
for(int i = 3; i <= n; i++) GCD = gcd(GCD, a[i] - a[i - 1]);
for(int i = 1; i <= m; i++)
{
cout << gcd(a[1] + b[i], GCD) << ' ';
}
}