1. 程式人生 > 實用技巧 >Codeforces Round #664 題解(A ~ C)

Codeforces Round #664 題解(A ~ C)

1395A - Boboniu Likes to Color Balls

如果在r,b,g,w中小於或等於一個奇數,則可以將其定為迴文。
否則,請進行一次操作(如果可以),然後檢查上述情況。
進行多次操作是沒有意義的,因為我們只關心r,b,g,w的奇偶性

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll r, g, b, w;
int main() {
	//freopen("in.txt", "r", stdin);
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int t; cin >> t; while (t--) {
		cin >> r >> g >> b >> w;
		ll c = r + g + b + w;
		int cnt = (r & 1) + (g & 1) + (b & 1);
		if (cnt == 0 || cnt == 3)cout << "Yes" << endl;
		else if(c & 1){
			if (cnt == 2 && r && g && b)w += 3, cnt = 1;
			
			if (cnt == 2) cout << "No\n";
			else if (w & 1) cout << "No\n";
			else cout << "Yes" << endl;
		} else cout << "No" << endl;
	}
}

1395B - Boboniu Plays Chess

假設\(f(i,j) =((i + Sx-2)modn + 1, (j + Sy-2)mod (m + 1))\)
將i從1迭代到n:如果i為奇數,則列印\(f(i,1),f(i,2),…,f(i,m)\)
否則列印\(f(i,m),f(i,m-1),…,f(i,1)\)

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

const int maxn = 100;
int n, m, a, b;
bool row[maxn + 3], vis[maxn + 3][maxn + 3];

int main() {
	scanf("%d %d %d %d", &n, &m, &a, &b);
	for (int i = 1; i <= n; i++) {
		if (i > 1) for (int j = 1; j <= n; j++) if (!row[j]) { a = j; break; }
		row[a] = true;
		printf("%d %d\n", a, b);
		vis[a][b] = true;
		for (int j = 1; j < m; j++) {
			for (int k = 1; k <= m; k++) if (!vis[a][k]) { b = k; break; }
			printf("%d %d\n", a, b);
			vis[a][b] = true;
		}
	}
	return 0;
}	

1395C - Boboniu and Bit Operations

假設答案為A。因此,對於所有\(i(1≤i≤n),c_i | A = A\)
由於\(a_i,b_i <2^9\),我們可以列舉從\(0到2^9-1\)的所有整數,並檢查每個i是否存在\(j(ai&bj)| A = A\)。 最少的就是答案。
時間複雜度為\(O(2^9⋅n^2\)

#include<bits/stdc++.h>
#define ci const int&
using namespace std;
int n,m,p[210],d[210],ans;
bool Check(ci x){
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j)if(((p[i]&d[j])|x)==x)goto Next;
		return 0;
		Next:;
	}
	return 1;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)scanf("%d",&p[i]);
	for(int i=1;i<=m;++i)scanf("%d",&d[i]);
	ans=(1<<9)-1;
	for(int i=8;i>=0;--i)Check(ans^(1<<i))?ans^=(1<<i):0;
	printf("%d",ans);
	return 0;
}