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 Z2d{\mathbb{Z_2^d}} vector space, where dd 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 Z2d{\mathbb{Z_2^d}}.

  • Relate the answers to the queries of various types with the basis of the vectors found in Part 1.

Important terms

Vector Space Z2d{\mathbb{Z_2^d}}

Z2{\mathbb{Z_2}}: Zm{\mathbb{Z_m}} is the set of remainders upon division by mm. Therefore, Z2{\mathbb{Z_2}} is the set of remainders upon division by 22: {0,1}\{0, 1\}.

Z2d{\mathbb{Z_2^d}}: It represents the set of all binary vectors of length dd, where each component of the vector belongs to the field Z2{\mathbb{Z_2}}.

Linear Span

The span of a set of vectors S={v1,v2,,vn}{S = \{v_1, v_2,\dots, v_n \}} in a vector space VV consists of all vectors xx that can be represented as linear combination of the vectors in SS. Mathematically, the span of SS is defined as:

span(s)={i=1nciviviS,ci{0,1}}.\begin{align*} \operatorname{span}(s) = \bigg\{\sum_{i=1}^{n} c_i v_i \bigg| v_i \in S, c_i \in \{0, 1\} \bigg\}. \end{align*}

This mean that any vector xx in VV can be expressed as a linear combination of the vectors v1,v2,,vnv_1, v_2,\dots,v_n in SS, where each coefficient cic_i is either 0 or 1. The span of SS represents the subspace of VV that is generated by the vectors in SS, 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 B={v1,v2,,vn}B = \{v_1, v_2,\dots,v_n\} is termed the basis of a vector space VV if the span of BB covers VV entirely and BB is linearly independent. In other words, any vector in VV can be expressed as a linear combination of the vectors in BB, and no vector in BB can be represented as a linear combination of the others. The number of vectors in BB , denoted as nn, is defined as the dimension of VV, represented by dim(V)\dim(V). 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 NN 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 AA and BB in the set, their bitwise xor ABA \oplus B 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 2basis.size()n2^{basis.size()}- n. Here 2basis.size()2^{basis.size()} represents the total number of distinct vectors that can be formed using the basis, including the zero vector. We subtract nn to adjust for the number of vectors already in the set.

N = int(input())
numbers = list(map(int, input().split()))
# compute basis size
basis_size = 0
for i in range(N):
if numbers[i]:
basis_size += 1 # add numbers[i] to basis
for j in range(i + 1, N): # reduce remaining vectors
numbers[j] = min(numbers[j], numbers[j] ^ numbers[i])
# print number of missing elements
print(2**basis_size - N)

You might be wondering why this method of reduction works. For instance, if the basis elements were currently [12,112][1_2, 11_2] and you tried inserting 10210_2, you'd end up with a result of 121_2. This is clearly wrong, since 10210_2 is not independent of the basis [12,112][1_2, 11_2].

However, the catch is that every other basis element must be reduced via the same method. This means [12,112][1_2, 11_2] is not actually a basis our algorithm can produce: properly reduced, it should be [12,102][1_2, 10_2] instead.

More generally, this reduction guarantees that, if bit xx 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

Official Editorial

Solution

Problems

Resources
Benq

8 related tasks

Some harder tasks:

StatusSourceProblem NameDifficultyTags
ACMedium
Show TagsXOR Basis
ACMedium
Show TagsXOR Basis
CFMedium
Show TagsXOR Basis
CFMedium
Show TagsBinary Jumping, Rerooting, XOR Basis
CFMedium
Show TagsBinary Jumping, XOR Basis
CFMedium
Show TagsXOR Basis
CFMedium
Show TagsXOR Basis
CFHard
Show TagsXOR Basis
CFVery Hard
Show TagsXOR Basis
CFVery Hard
Show TagsXOR Basis
ACVery 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!