思路:
线性基,线性基的每个元素尽可能小
将k转换成二进制与排好序的线性基相对应
如果线性基的个数小于n,说明n个元素线性相关,所以可以构成0,k要减1
代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair<LL, LL> #define pii pair<int, int> #define piii pair<pii, int> #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head vector<LL> base; int main() { int T, n, q; LL t; scanf("%d", &T); for (int cs = 1; cs <= T; cs++) { scanf("%d", &n); base.clear(); for (int i = 0; i < n; i++) { scanf("%lld", &t); for (int j = 0; j < base.size(); j++) t = min(t, t^base[j]); if(t) base.pb(t); } sort(base.begin(), base.end()); int m = base.size(); for (int i = 1; i < m; i++) { for (int j = i-1; j >= 0; j--) { base[i] = min(base[i], base[i]^base[j]); } } scanf("%d", &q); printf("Case #%d: ", cs); while(q--) { scanf("%lld", &t); if(m < n) t--; if(t >= (1LL << m)) { printf("-1 "); continue; } LL ans = 0; for (int j = 0; j < m; j++) if((t>>j)&1) ans ^= base[j]; printf("%lld ", ans); } } return 0; }