我是跟着 Menci 的 博客 学的。写出来的模板如下:
struct LinearBasis {
int a[65];
void insert(int t) {
for (int i = 60; i >= 0; i--)
if ((1LL << i) & t) {
if (a[i] != 0)
t ^= a[i];
else {
for (int j = 0; j < i; j++) if ((1LL << j) & t) t ^= a[j];
for (int j = i + 1; j <= 60; j++) if ((1LL << i) & a[j]) a[j] ^= t;
a[i] = t;
return;
}
}
}
LL qmax(LL x) {
LL res = x;
for (int i = 0; i <= 60; i++) chmax(res, res ^ a[i]);
return res;
}
} lb;
但是我看大多数人的写法都没有
for (int j = 0; j < i; j++) if ((1LL << j) & t) t ^= a[j];
for (int j = i + 1; j <= 60; j++) if ((1LL << i) & a[j]) a[j] ^= t;
这两行。
没有这两行,怎么能保证 a 中只有第 aj 的第 j 位是 1 呢?