-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasis.cpp
More file actions
38 lines (33 loc) · 862 Bytes
/
Basis.cpp
File metadata and controls
38 lines (33 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
template<typename T, int SZ>
struct Basis {
int bits;
array<int, 20> basis {};
Basis() : bits(SZ) {}
void Add(T x) {
for (int i = bits-1; i >= 0 and x > 0; --i) {
if (basis[i]) x = min(x, x ^ basis[i]);
else {basis[i] = x; x = 0;}
}
}
void Merge(const Basis &other) {
for (int i = bits-1; i >= 0; --i) {
if (!other.basis[i]) break;
Add(other.basis[i]);
}
}
bool isPossible(T k) const {
for (int i = bits-1; i >= 0; --i) {
k = min(k, k ^ basis[i]);
}
return k == 0;
}
T max_xor(){
T res = 0;
for(int i = bits - 1; i >= 0; i--)
if(!((res >> i) & 1) && basis[i])
res ^= basis[i];
return res;
}
};
using T = Basis<int, 20>;
// link: https://codeforces.com/gym/493310/submission/237680600