Codeforces Round #691 (Div. 2)

A. Red-Blue Shuffle

题目
There are n cards numbered 1,…,n. The card i has a red digit ri and a blue digit bi written on it.

We arrange all n cards in random order from left to right, with all permutations of 1,…,n having the same probability. We then read all red digits on the cards from left to right, and obtain an integer R. In the same way, we read all blue digits and obtain an integer B. When reading a number, leading zeros can be ignored. If all digits in a number are zeros, then the number is equal to 0. Below is an illustration of a possible rearrangement of three cards, and how R and B can be found.
在这里插入图片描述
Two players, Red and Blue, are involved in a bet. Red bets that after the shuffle R>B, and Blue bets that R<B. If in the end R=B, the bet results in a draw, and neither player wins.

Determine, which of the two players is more likely (has higher probability) to win the bet, or that their chances are equal. Refer to the Note section for a formal discussion of comparing probabilities.

Input
The first line contains a single integer T (1≤T≤100) — the number of test cases.

Descriptions of T test cases follow. Each test case description starts with a line containing a single integer n (1≤n≤1000) — the number of cards.

The following line contains a string of n digits r1,…,rn — red digits on cards 1,…,n respectively.

The following line contains a string of n digits b1,…,bn — blue digits on cards 1,…,n respectively.

Note that digits in the same line are not separated with any delimiters.

Output
Print T answers for the test cases in order, one per line.

If Red has a strictly higher change to win, print "RED".

If Blue has a strictly higher change to win, print "BLUE".

If both players are equally likely to win, print "EQUAL".

Note that all answers are case-sensitive.

Example
inputCopy
3
3
777
111
3
314
159
5
09281
09281
outputCopy
RED
BLUE
EQUAL
Note
Formally, let nR be the number of permutations of cards 1,…,n such that the resulting numbers R and B satisfy R>B. Similarly, let nB be the number of permutations such that R<B. If nR>nB, you should print "RED". If nR<nB, you should print "BLUE". If nR=nB, print "EQUAL".

In the first sample case, R=777 and B=111 regardless of the card order, thus Red always wins.

In the second sample case, there are two card orders when Red wins, and four card orders when Blue wins:

order 1,2,3: 314>159;
order 1,3,2: 341>195;
order 2,1,3: 134<519;
order 2,3,1: 143<591;
order 3,1,2: 431<915;
order 3,2,1: 413<951.
Since R<B is more frequent, the answer is "BLUE".

In the third sample case, R=B regardless of the card order, thus the bet is always a draw, and both Red and Blue have zero chance to win.

题意
有n张牌,每张上面有两个数字,排列这些牌之后比较上面n个数字的大小放在一起和下面n个数字放在一起的大小,如果上面大的数量多,RED获胜,如果相同则EQUAL 否则BLUE获胜

思路
直接比较上面和下面的差值谁大即可, 刚开始想错了,统计了所有排列。

代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		string s,t;
		cin>>s>>t;
		if(s==t)
		{
			cout<<"EQUAL\n";
			continue;
		}
		int len = s.size();
		long long ans1 = 0 , ans2 = 0;
		for(int i=0;i<len;i++)
		{
			if(s[i]>t[i])
			ans1++;
			else if(t[i]>s[i])
			ans2++;
		}
		if(ans1 > ans2)
		cout<<"RED\n";
		else if(ans1 == ans2)
		cout<<"EQUAL\n";
		else
		cout<<"BLUE\n";
	}
} 

B. Move and Turn

题目
A robot is standing at the origin of the infinite two-dimensional plane. Each second the robot moves exactly 1 meter in one of the four cardinal directions: north, south, west, and east. For the first step the robot can choose any of the four directions, but then at the end of every second it has to turn 90 degrees left or right with respect to the direction it just moved in. For example, if the robot has just moved north or south, the next step it takes has to be either west or east, and vice versa.

The robot makes exactly n steps from its starting position according to the rules above. How many different points can the robot arrive to at the end? The final orientation of the robot can be ignored.

Input
The only line contains a single integer n (1≤n≤1000) — the number of steps the robot makes.

Output
Print a single integer — the number of different possible locations after exactly n steps.

Examples
inputCopy
1
outputCopy
4
inputCopy
2
outputCopy
4
inputCopy
3
outputCopy
12
Note
In the first sample case, the robot will end up 1 meter north, south, west, or east depending on its initial direction.

In the second sample case, the robot will always end up 2–√ meters north-west, north-east, south-west, or south-east.

题意
在笛卡尔直角坐标系上走n步,第一步可以往四个方面走,其他每一步都只能往左往右拐,问n步后能到达的点有多少个。

思路
暴力画图推规律。

在这里插入图片描述
可以推出
1 4
2 4
3 12
4 9
5 24
6 16
7 40
8 25
9 60
10 36

容易发现
偶数的时候是\(3^2\) 开始的,每下一项\((n+1)^2\) 的一个数列
奇数的时候是从4开始的每次之间的差递增4的数列

代码

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

typedef long long ll;

long long qpow(ll a,ll b)
{
	ll base = a,ans = 1;
	while(b)
	{
		if(b&1)
		ans *= base;
		base *= base;
		b >>= 1;
	}
	return ans;
}
int main()
{
	int n;
	cin>>n;
	if(n==1)
	cout<<4<<"\n";
	else if(n==2)
	cout<<4<<"\n";
	else
	{
		if(n&1)
		{
			ll now = 8;
			ll ans = 4;
			for(int i=3;i<=n;i+=2)
			{
				ans += now;
				now += 4;
			}
			cout<<ans<<"\n";
		}
		else
		{
			ll ans = 2;
			for(int i=4;i<=n;i+=2)
			ans ++;
			cout<<qpow(ans,2)<<"\n";
		}
	}
}

C. Row GCD

题目
You are given two positive integer sequences \(a1,…,an\) and \(b1,…,bm\). For each j=1,…,m find the greatest common divisor of \(a1+bj,…,an+bj\).

Input
The first line contains two integers n and m (1≤n,m≤2⋅\(10^5\)).

The second line contains n integers a1,…,an (1≤ai≤\(10^{18}\)).

The third line contains m integers b1,…,bm (1≤bj≤\(10^{18}\)).

Output
Print m integers. The j-th of them should be equal to GCD(a_1+bj,…,a_n+bj).

Example
inputCopy
4 4
1 25 121 169
1 2 7 23
outputCopy
2 3 8 24

题意
给一个长度为n的数组a和长度为m的数组b 每次吧a数组每个数都加上b[j] 后求当前a的gcd

思路
首先我们需要知道一个gcd的基本性质 :\(gcd(a,b)=gcd(a-b,b)\)
知道了这个之后我们把\((a_1+b_j,…,a_n+b_j)\)每一项从第二项开始后一项减去前一项,a的GCD不变,现在就只有\(a_1+b_j\)这一项没有被减去过任何数,于是我们先求差分数组的GCD,再每一项与\(a_1+b_j\)求GCD就是最后的答案

代码

#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
 
const int maxn = 200000+100;
 
ll a[maxn] , b[maxn] , tool[maxn];
 
ll gcd(ll a,ll b)
{
	return b==0?a:gcd(b,a%b);
}
int main()
{
//	cout<<__gcd(30000000000000,6000000000000)<<"\n";
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
	sort(a+1,a+1+n);
	for(int i=1;i<=m;i++) scanf("%lld",&b[i]);
	for(int i=1;i<=n-1;i++)
	tool[i] = a[i+1] - a[i];
	ll g = tool[1];
	for(int i=2;i<=n-1;i++)
	{
		g = __gcd(g,tool[i]);
	}
//	cout<<g<<"\n";
	for(int i=1;i<=m;i++)
	{
		cout<<__gcd(g,b[i] + a[1])<<" ";
	}
}
原文地址:https://www.cnblogs.com/liangyj/p/14161864.html