12tqian's Competitive Programming Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub 12tqian/cp-library

:heavy_check_mark: XOR Basis
(library/numerical/xor-basis.hpp)

XOR Basis

Functions

Resources

Verified with

Code

#pragma once

void full_reduce(std::vector<int>& b) {
	int n = (int)b.size();
	for (int i = n - 1; i >= 0; i--) {
		for (int j = i - 1; j >= 0; j--) {
			b[j] = std::min(b[j], b[j] ^ b[i]);
		}
	}
}

template <class T> T reduce(std::vector<T>& b, T x) {
	for (auto& t : b) {
		x = std::min(x, x ^ t);
	}
	return x;
}

template <class T> bool add(std::vector<T>& b, T x) {
	if (!(x = reduce(b, x))) return false;
	int ind = 0;
	while (ind < (int)b.size() && b[ind] > x) ind++;
	b.insert(b.begin() + ind, x);
	return true;
}
void full_reduce(std::vector<int>& b) {
	int n = (int)b.size();
	for (int i = n - 1; i >= 0; i--) {
		for (int j = i - 1; j >= 0; j--) {
			b[j] = std::min(b[j], b[j] ^ b[i]);
		}
	}
}

template <class T> T reduce(std::vector<T>& b, T x) {
	for (auto& t : b) {
		x = std::min(x, x ^ t);
	}
	return x;
}

template <class T> bool add(std::vector<T>& b, T x) {
	if (!(x = reduce(b, x))) return false;
	int ind = 0;
	while (ind < (int)b.size() && b[ind] > x) ind++;
	b.insert(b.begin() + ind, x);
	return true;
}
Back to top page