Skip to content

Commit 0d59872

Browse files
committed
feat: added XORBasis.kth()
1 parent 8256aec commit 0d59872

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

src/alfred/math/linear.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ struct XORBasis {
146146
return i;
147147
} else x ^= p[i];
148148
}
149+
has_zero = true;
149150
return -1;
150151
}
151152
inline T max(T ans = 0) {
@@ -161,6 +162,31 @@ struct XORBasis {
161162
return ans;
162163
}
163164
inline int size(void) { return siz; }
165+
inline void rebuild(void) {
166+
for (int i = C - 1; i >= 0; i--) {
167+
for (int j = 0; j < i; j++) {
168+
if (p[i] >> j & 1) p[i] ^= p[j];
169+
}
170+
}
171+
}
172+
inline T kth(size_t k) { // kth minimum
173+
rebuild();
174+
std::vector<T> narr;
175+
for (int i = 0; i < C; i++) {
176+
if (p[i] != 0) narr.push_back(p[i]);
177+
}
178+
T ans = 0;
179+
assert(k >= 1);
180+
assert(narr.size() == (size_t)siz);
181+
if (k > 1 || !zero) {
182+
k -= zero;
183+
assert(k < (1ull << siz));
184+
for (int i = siz - 1; i >= 0; i--) {
185+
if (k >> i & 1) ans ^= narr[i];
186+
}
187+
}
188+
return ans;
189+
}
164190
};
165191

166192
#endif // !AFMT_LINEAR

0 commit comments

Comments
 (0)