考虑 DP。
表示第 轮开始,变量值 ,双方用最优策略, 是否必然发生。( 为必然)
初始:,其他 。
转移:
答案:。
复杂度太高,考虑优化。
为真的 dp 构成一个集合。具体地,。
初始: 。
转移:
(其中 )
上述可用线性基维护。
详细地,令 , 是线性子空间(异或封闭), 是偏移量。
当 ,即 ,讨论 种情况:
-
,有 ,集合不变。
-
,并集为 。 表示合并 和 后的新空间。
代码上,相当于将 插入线性基,偏移量不变。
当 ,即 ,同样 种:
-
,同理,集合不变。
-
,则 与 无交集,即 。
如此之后任意操作集合保持 不变,dp 全为 。
观察到偏移量 不变,恒为 。
最终检查 。因为只要不为空集合一定包含 (初始为 ),则等价于检查 。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 202;
int n;
long long a[N];
string s;
struct linear_basis { // 线性基
long long base[60];
linear_basis() {
memset(base, 0, sizeof(base));
}
bool insert(long long x) {
for (int i = 59; i >= 0; --i) {
if ((x >> i) & 1) {
if (!base[i]) {
base[i] = x;
return 1;
}
x ^= base[i];
}
}
return 0;
}
bool contains(long long x) {
for (int i = 59; i >= 0; --i) {
if ((x >> i) & 1) {
if (!base[i]) {
return 0;
}
x ^= base[i];
}
}
return 1;
}
};
bool solve() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
cin >> s;
linear_basis lb;
for (int i = n - 1; i >= 0; --i) {
if (s[i] == '0') {
lb.insert(a[i]); // 插入
} else {
if (!lb.contains(a[i])) { // 检查存在性
return 1; // 不存在,那么空集,后续 dp 恒为 0,答案为 1
}
}
}
return 0; // 说明不是空集,答案为 0
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--) {
cout << solve() << '\n';
}
return 0;
}