跳到正文

题解

题解 [AGC045A] Xor Battle

Jsxts_约 3 分钟
原文
文章信息与版本

文章信息

发布
2023年3月8日
收录
2026年3月31日
更新
2026年11月11日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

线性基好题。

考虑从后往前枚举,设 SiS_i 表示前 ii 步做完后 00 必胜的 xx 集合。

那么当 pi=0p_i=0 时, SiS_i 相当于 Si+1S_{i+1} 并上 Si+1S_{i+1} 中所有数与 aia_i 的异或值,即 Si=Si+1∪{x⊕ai∣x∈Si+1}S_i=S_{i+1}\cup \{x\oplus a_i|x\in S_{i+1}\}。而当 pi=1p_i=1 时,Si={x∣x∈Si+1S_i=\{x|x\in S_{i+1} 且 x⊕ai∈Si+1}x \oplus a_i\in S_{i+1}\}。

根据 SS 的性质,发现 SS 是所有 pi=0p_i=0 的 aia_i 取若干个异或得到的数的集合,并且若 x∈Si,y∈Six\in S_i,y\in S_i,则 x⊕y∈Six\oplus y\in S_i,所以当 pi=1p_i=1 时,如果 ai∈Si+1a_i\in S_{i+1},则所有 x⊕ai∈Si+1x\oplus a_i\in S_{i+1},即 Si=Si+1S_i=S_{i+1}。而当 ai∉Si+1a_i\notin S_{i+1} 时,对于任意的 xx,xx 和 x⊕aix\oplus a_i 不可能同时在集合里(由上面性质),所以 Si=∅S_i=\emptyset,即 11 获胜。

我们可以维护集合 SS 的线性基,这样插入和查询是否 ai∉Si+1a_i\notin S_{i+1} 都很方便。

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 2e9;
inline ll read() {
	ll s = 0,f = 1;char ch = getchar();
	while (!isdigit(ch)) f = ch == '-' ? -1 : 1, ch = getchar();
	while (isdigit(ch)) s = (s << 3) + (s << 1) + ch - '0', ch = getchar();
	return s*f;
}
ll a[210];
struct Linear_Basis {
	ll p[65];
	bool ins(ll x) {
		for (int i = 62;~i;i -- ) {
			if (!(x >> i & 1)) continue;
			if (!p[i]) {
				p[i] = x;
				return 1;
			}
			x ^= p[i];
		}
		return 0;
	}
}L;
char s[210];
int main() {
	int T = read();
	while (T -- ) {
		int n = read();
		for (int i = 1;i <= n;i ++ ) a[i] = read();
		scanf("%s",s+1);
		for (int i = 0;i <= 62;i ++ ) L.p[i] = 0;
		for (int i = n;i;i -- ) {
			if (L.ins(a[i]) && s[i] == '1') {
				puts("1");
				goto bre;
			}
		}
		puts("0");
		bre:;
	}
	return 0;
}
原文