题解 [AGC045A] Xor Battle

· · 题解

线性基好题。

考虑从后往前枚举,设 S_i 表示前 i 步做完后 0 必胜的 x 集合。

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

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

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

#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;
}