Notice that if we can erase the sequence using more than operations then we can also erase it using operations. Why? That’s because . So we just have to check if the sequence can be erased using or operations.
If the XOR of elements of the whole sequence is , then we can erase it using operations.
We can erase the sequence using exactly operations iff -
There exists a non-empty subset of such that the XOR of the elements of that subsequence is .
We can check the second condition in using the basis vectors of the elements of and checking if is in the same subspace. Check this to know more.
So the problem can be solved in .
Code(C++):
#include<bits/stdc++.h>
using namespace std;
struct Basis {
vector<int> a;
void insert(int x) {
for (auto &i: a) x = min(x, x ^ i);
if (!x) return;
for (auto &i: a) if ((i ^ x) < i) i ^= x;
a.push_back(x);
sort(a.begin(), a.end());
}
bool can(int x) {
for (auto &i: a) x = min(x, x ^ i);
return !x;
}
}t;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q; cin >> n >> q;
int tot = 0;
for (int i = 1; i <= n; i++) {
int x; cin >> x;
tot ^= x;
t.insert(x);
}
while (q--) {
int x; cin >> x;
if (tot == x) {
cout << 1 << '\n';
}
else if (tot == 0 and t.can(x)) {
cout << 2 << '\n';
}
else {
cout << -1 << '\n';
}
}
return 0;
}