Resources
| Resources | |||||
|---|---|---|---|---|---|
| CF | inspiration for below | ||||
| Benq | used at USACO Camp | ||||
| Hoffman + Kunze | prerequisites for this topic | ||||
Introduction
An XOR basis is a minimal set of linearly independent binary vectors that can represent any vector in a given set through XOR combinations. In computational problems, constructing an XOR basis involves iteratively adding vectors to the basis while ensuring each new vector remains independent by reducing it with existing basis vectors. This basis allows efficient representation and manipulation of binary vector spaces, enabling quick determination of linear independence and facilitating solutions to various optimization and combinatorial problems.
XOR basis involves two parts:
Represent each given number in its base 2 form, considering it as a vector in the vector space, where is the maximum possible number of bits. The XOR operation on these numbers is equivalent to the addition of the corresponding vectors in the vector space .
Relate the answers to the queries of various types with the basis of the vectors found in Part 1.
Important terms
Vector Space
: is the set of remainders upon division by . Therefore, is the set of remainders upon division by : .
: It represents the set of all binary vectors of length , where each component of the vector belongs to the field .
Linear Span
The span of a set of vectors in a vector space consists of all vectors that can be represented as linear combination of the vectors in . Mathematically, the span of is defined as:
This mean that any vector in can be expressed as a linear combination of the vectors in , where each coefficient is either 0 or 1. The span of represents the subspace of that is generated by the vectors in , encompassing all possible combinations of those vectors. Understanding the span of a set of vectors is crucial for determining the reach or extent of the vector's influence within the vector space.
Basis
A set of vectors is termed the basis of a vector space if the span of covers entirely and is linearly independent. In other words, any vector in can be expressed as a linear combination of the vectors in , and no vector in can be represented as a linear combination of the others. The number of vectors in , denoted as , is defined as the dimension of , represented by . Understanding the basis and dimension of a vector space is crucial for analyzing its structure, solving linear equations, and performing transformations in various mathematical and computational contexts.
Example - Xor Closure
Focus Problem – try your best to solve this problem before continuing!
You are given a set of integer values. You should find the minimum number of values that you need to add to the set such that the following will hold true:
- For every two integers and in the set, their bitwise xor is also in the set.
Solution
The solution involves constructing an XOR basis from a set of binary vectors and computing a value based on this basis. Each vector is inserted into the XOR basis by attempting to minimize it through XOR-ing with existing basis vectors, ensuring that the vector remains linearly independent. If the vector cannot be fully reduced to zero, it is added to the basis. This ensures that the basis only contains the minimal set of vectors needed to represent the space spanned by the input vectors.
Once the basis is constructed, the final result is calculated as . Here represents the total number of distinct vectors that can be formed using the basis, including the zero vector. We subtract to adjust for the number of vectors already in the set.
N = int(input())numbers = list(map(int, input().split()))# compute basis sizebasis_size = 0for i in range(N):if numbers[i]:basis_size += 1 # add numbers[i] to basisfor j in range(i + 1, N): # reduce remaining vectorsnumbers[j] = min(numbers[j], numbers[j] ^ numbers[i])# print number of missing elementsprint(2**basis_size - N)
You might be wondering why this method of reduction works. For instance, if the basis elements were currently and you tried inserting , you'd end up with a result of . This is clearly wrong, since is not independent of the basis .
However, the catch is that every other basis element must be reduced via the same method. This means is not actually a basis our algorithm can produce: properly reduced, it should be instead.
More generally, this reduction guarantees that, if bit is the MSB of one basis element, it must be switched off in every basis element after it: this is why this method of reduction works without having to maintain the basis vectors in sorted order.
Example - Xor Cycle
Focus Problem – try your best to solve this problem before continuing!
Solution
Solution
Problems
| Resources | |||||
|---|---|---|---|---|---|
| Benq | 8 related tasks | ||||
Some harder tasks:
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| AC | Medium | Show TagsXOR Basis | ||||
| AC | Medium | Show TagsXOR Basis | ||||
| CF | Medium | Show TagsXOR Basis | ||||
| CF | Medium | Show TagsBinary Jumping, Rerooting, XOR Basis | ||||
| CF | Medium | Show TagsBinary Jumping, XOR Basis | ||||
| CF | Medium | Show TagsXOR Basis | ||||
| CF | Medium | Show TagsXOR Basis | ||||
| CF | Hard | Show TagsXOR Basis | ||||
| CF | Very Hard | Show TagsXOR Basis | ||||
| CF | Very Hard | Show TagsXOR Basis | ||||
| AC | Very Hard | Show TagsXOR Basis | ||||
Module Progress:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!