bzoj 4269 再见Xor 线性基
题面
解法
第一问就是线性基的裸题
第二问也很类似,从低位向高位枚举,如果线性基上这一位有数,那么直接异或后返回
时间复杂度:\(O(n\ log\ a_i)\)
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
x = 0; int f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
struct Linear_Base {
int a[40];
void ins(int x) {
for (int i = 31; ~i; i--)
if ((x >> i) & 1) {
if (!a[i]) {a[i] = x; break;}
x ^= a[i];
}
}
} b;
main() {
int n; read(n);
for (int i = 1; i <= n; i++) {
int x; read(x);
b.ins(x);
}
int mx = 0;
for (int i = 31; ~i; i--)
if ((mx ^ b.a[i]) > mx) mx ^= b.a[i];
int mx2 = mx;
for (int i = 0; i <= 31; i++)
if (b.a[i]) {mx2 ^= b.a[i]; break;}
cout << mx << ' ' << mx2 << "\n";
return 0;
}