Skip to content

Instantly share code, notes, and snippets.

@hikariyo
Created January 29, 2026 09:01
Show Gist options
  • Select an option

  • Save hikariyo/0f202a2df9af9ddcdce1b2a24e4f0b68 to your computer and use it in GitHub Desktop.

Select an option

Save hikariyo/0f202a2df9af9ddcdce1b2a24e4f0b68 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
struct Basis {
i64 b[60];
int cnt;
bool zero;
void clear() {
memset(b, 0, sizeof b);
zero = false;
cnt = 0;
}
bool ins(i64 x) {
for (int i = 59; i >= 0; i--) {
if (x >> i & 1) {
if (!b[i]) {
b[i] = x;
cnt++;
return true;
}
x ^= b[i];
}
}
zero = true;
return false;
}
i64 qry(i64 k) {
if (k > (1ll << cnt)) return -1;
i64 ans = 0;
int nowcnt = cnt;
for (int i = 59; i >= 0; i--) {
if (!b[i]) continue;
nowcnt--;
if (k > (1ll << nowcnt)) {
k -= 1ll << nowcnt;
if (!(ans >> i & 1)) ans ^= b[i];
}
else {
if (ans >> i & 1) ans ^= b[i];
}
}
return ans;
}
} B;
void solve() {
int n, Q;
cin >> n;
B.clear();
for (int i = 1; i <= n; i++) {
i64 x;
cin >> x;
B.ins(x);
}
cin >> Q;
for (int i = 1; i <= Q; i++) {
i64 k;
cin >> k;
if (!B.zero) k++;
cout << B.qry(k) << '\n';
}
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T = 1;
cin >> T;
for (int i = 1; i <= T; i++) {
cout << "Case #" << i << ":\n";
solve();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment