Loading

Codeforces Round #770 (Div. 2) ABC

A. Reverse and Concatenate

如果k为0直接输出1,否则如果s为回文串的话输出1,其他情况输出2(经过一次变换后必然为回文)。注意不能特判k = 1的情况,用这个样例hack了一发23333

#include <bits/stdc++.h>
#define pii pair<int,int>
#define pb push_back
using namespace std;
int n, k;
void solve() {
	cin >> n >> k;
	string s;
	cin >> s;
	if(k == 0) {
		cout << 1 << endl;
		return;
	} else {
		string ss = s;
		reverse(ss.begin(), ss.end());
		if(ss == s) cout << 1 << endl;
		else cout << 2 << endl;
		return;
	}
}
int main() {
	int T = 1;
	cin >> T;
	while(T--) {
		solve();
	}
	return 0;
}
// 1
// 2 1
// ab

B. Fortune Telling

Your friends Alice and Bob practice fortune telling.

Fortune telling is performed as follows. There is a well-known array 𝑎a of 𝑛n non-negative integers indexed from 11 to 𝑛n. The tellee starts with some non-negative number 𝑑d and performs one of the two operations for each 𝑖=1,2,…,𝑛i=1,2,…,n, in the increasing order of 𝑖i. The possible operations are:

  • replace their current number 𝑑d with 𝑑+𝑎𝑖d+ai
  • replace their current number 𝑑d with 𝑑⊕𝑎𝑖d⊕ai (hereinafter ⊕⊕ denotes the bitwise XOR operation)

Notice that the chosen operation may be different for different 𝑖i and for different tellees.

One time, Alice decided to start with 𝑑=𝑥d=x and Bob started with 𝑑=𝑥+3d=x+3. Each of them performed fortune telling and got a particular number in the end. Notice that the friends chose operations independently of each other, that is, they could apply different operations for the same 𝑖i.

You learnt that either Alice or Bob ended up with number 𝑦y in the end, but you don't know whose of the two it was. Given the numbers Alice and Bob started with and 𝑦y, find out who (Alice or Bob) could get the number 𝑦y after performing the operations. It is guaranteed that on the jury tests, exactly one of your friends could have actually gotten that number.

Hacks

You cannot make hacks in this problem.

这个题题干很唬人,但发现问的比较特殊,并没有问Alice能否取到k,而是问Alice和Bob有一个人必然能取到问是哪一个。再加上这是B题的位置,往简单考虑的话就能想到奇偶性了。对于二进制的最低位,加法和异或实际上是一样的,因此只要判断x的最低位经过了这些运算以后能否变为y的最低位,能的话输出Alice,反之输出Bob。

#include <bits/stdc++.h>
#define pii pair<int,int>
#define pb push_back
#define ll long long
using namespace std;
ll n, x, y, a[100005];
void solve() {
	cin >> n >> x >> y;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	int d1 = x, d2 = x + 3;
	int cnt = 0;
	for(int i = 1; i <= n; i++) {
		cnt += (a[i] & 1);
	}
	int low = x & 1;
	if(low ^ (cnt & 1)) {
		if(y & 1) {
			puts("Alice");
		} else {
			puts("Bob");
		}
	} else {
		if(y % 2 == 0) {
			puts("Alice");
		} else {
			puts("Bob");
		}
	}
}
int main() {
	int T = 1;
	cin >> T;
	while(T--) {
		solve();
	}
	return 0;
}

C. OKEA

You work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!

The department you work in sells 𝑛⋅𝑘n⋅k items. The first item costs 11 dollar, the second item costs 22 dollars, and so on: 𝑖i-th item costs 𝑖idollars. The items are situated on shelves. The items form a rectangular grid: there are 𝑛n shelves in total, and each shelf contains exactly 𝑘k items. We will denote by 𝑎𝑖,𝑗ai,j the price of 𝑗j-th item (counting from the left) on the 𝑖i-th shelf, 1≤𝑖≤𝑛,1≤𝑗≤𝑘1≤i≤n,1≤j≤k.

Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items 𝑎𝑖,𝑙,𝑎𝑖,𝑙+1,…,𝑎𝑖,𝑟ai,l,ai,l+1,…,ai,r for some shelf 𝑖i and indices 𝑙≤𝑟l≤r? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.

You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array 𝑎a that:

  • Every number from 11 to 𝑛⋅𝑘n⋅k (inclusively) occurs exactly once.
  • For each 𝑖,𝑙,𝑟i,l,r, the mean price of items from 𝑙l to 𝑟r on 𝑖i-th shelf is an integer.

Find out if such an arrangement is possible, and if it is, give any example of such arrangement.

Input

The first line contains a single integer 𝑡t (1≤𝑡≤5001≤t≤500) — the number of test cases.

The first and only line of each test case contains two integers 𝑛n and 𝑘k (1≤𝑛,𝑘≤5001≤n,k≤500) — the number of shelves and length of each shelf, respectively.

It is guaranteed that the sum 𝑛n over all test cases does not exceed 500500 and the sum 𝑘k over all test cases does not exceed 500500.

Output

Print the answer for each test case.

If such an arrangement exists, print "YES" on a single line. After that, print any example on 𝑛n lines of 𝑘k numbers each, one line per shelf. Each number from 11 to 𝑛⋅𝑘n⋅k must occur exactly once in the output.

If no good arrangement exists, print a single word "NO" on its own line.

Example

input

Copy

4
1 1
2 2
3 3
3 1

output

Copy

YES
1 
YES
1 3 
2 4 
NO
YES
1 
2 
3 

一开始构造可能没什么思路,但是想到相邻的奇数求平均或者相邻的偶数求平均一定是能整除的(可以用等差数列公式证明),因此考虑这样构造。输出NO的情况为奇数/偶数个数不能被k整除(这样就没法平均分为若干个大小为k的组了)。正确性暂时还不知道如何证明~~

#include <bits/stdc++.h>
#define pii pair<int,int>
#define pb push_back
using namespace std;
int n, k;
int a[1005][1005];
void solve() {
	cin >> n >> k;
	if((n * k / 2) % k != 0) {
		puts("NO");
		return;
	}
	puts("YES");
	vector<int> a, b;
	for(int i = 1; i <= n * k; i++) {
		if(i & 1) a.pb(i);
		else b.pb(i);
	}
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= k; j++) {
			if(a.size()) {
				cout << a[a.size() - 1] << " ";
				a.pop_back();
			} else {
				cout << b[b.size() - 1] << " ";
				b.pop_back();
			}
			
		} cout << endl;
	}
}
int main() {
	int T = 1;
	cin >> T;
	while(T--) {
		solve();
	}
	return 0;
}
posted @ 2022-02-07 01:26  脂环  阅读(128)  评论(0编辑  收藏  举报