题解:AT_abc249_g [ABC249G] Xor Cards
Solution
将
于是我们花费了
可以发现,
-
- 如果
rs\oplus y_i>rs 且sm\oplus x_i 在B 中异或最终可以\le k ,那么取; - 否则不取。
注意判无解:强制取一个数
处理出后缀线性基,复杂度
Code
#include <bits/stdc++.h>
#define REP(i, l, r) for (int i = (l); i <= (r); ++ i)
#define DEP(i, r, l) for (int i = (r); i >= (l); -- i)
#define fi first
#define se second
#define pb emplace_back
#define mems(x, v) memset((x), (v), sizeof(x))
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
using namespace std;
namespace Milkcat {
typedef long long LL;
typedef pair<LL, LL> pii;
const int N = 1e6 + 5, P = 60;
struct LBase {
LL b[P + 3]; Linear() { mems(b, 0); }
int ins(LL x) { DEP(i, P, 0) if (x >> i & 1) if (b[i]) x ^= b[i]; else return b[i] = x, i; return 0; }
LL askmx(LL x) { DEP(i, P, 0) x = max(x, x ^ b[i]); return x; }
LL askmn(LL x) { DEP(i, P, 0) x = min(x, x ^ b[i]); return x; }
} C, B[P + 5];
LL n, m, k, x, ck, a[N], b[N];
vector<pii> s;
bool chk(int t, LL v) { return (B[t].askmn(v) <= k); }
int main() {
cin >> n >> k;
REP(i, 1, n) {
cin >> a[i] >> b[i];
int x = C.ins(b[i] << 30 | a[i]);
if (!x && !SZ(s)) s.pb(0, 0);
}
REP(i, 0, P) {
LL x = C.b[i];
if (x > 0) s.pb(x >> 30, x & ((1 << 30) - 1));
}
sort(ALL(s), greater<pii>()), m = SZ(s) - 1;
DEP(i, m, 0) B[i] = B[i + 1], B[i].ins(s[i].se);
REP(i, 0, m) ck |= chk(i + 1, s[i].se);
if (!ck) { cout << "-1\n"; return 0; }
LL rs = 0, sm = 0;
REP(i, 0, m) {
auto [x, y] = s[i];
int c1 = chk(i + 1, sm ^ y), c2 = chk(i + 1, sm);
if ((c1 && (rs ^ x) > rs) || !c2) rs ^= x, sm ^= y;
}
cout << rs << '\n';
return 0;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T = 1;
while (T --) Milkcat::main();
return 0;
}