[ABC274Ex] XOR Sum of Arrays Tutorial

· · 题解

To determine the lexicographical order of two sequences, one can firstly evaluate the length of their longest common prefix, then compare the first different element of the sequences. With binary search, one only needs to check whether two sequences are identical, then the whole problem can be easily solved.

Consider hashing. One can easily evaluate the hash value of a subarray of a sequence by precomuting all the hash values of the prefixes, provided that the hash is a polynomial hash. However, one cannot find the hash value of the bitwise exclusive or of two sequences by the hash value of these sequences.

Therefore, one may want to find some operation that is distributive on bitwise exclusive or. If there is any, one can use the operation and bitwise exclusive or to build something analogous to a polynomial hash, which means the new hash value of the bitwise exclusive or of two sequences is exactly the bitwise exclusive or of the hash value of the two sequences.

In fact, any Galois field of order 2, denoted as \mathbb F_{2^k}, is exactly what is desired. Take \mathbb F_{2^{62}} as an example, one way to evalutate the product in \mathbb F_{2^{62}} is to represent numbers as polynomials whose degree is no more than 61 and coefficients in \mathbb F_2, and take a prime polynomial as the modulus when computing products. One such polynomial is x^{62}+x^6+x^5+x^3+1.

The whole time complexity is O(Nw+Qw\log N), where w denotes the size of the field is 2^w. The probability of collusion is 2^{-w}.