Official

Ex - A Nameless Counting Problem Editorial by en_translator


Let’s say a sequence \(A\) of length \(L\) is a good sequence of length \(L\) if:

  • \(0 \leq A_i \leq M\) for all \(i = 1, 2, \ldots, L\) and
  • \(A_1 \oplus A_2 \oplus \cdots \oplus A_L = X\).

Note that the conditions are different from those in Problem Statement.

[1] Find the number of good sequences

Let us find \(f(L)\), the number of good sequences of length \(L\), for all \(L = 0, 1, 2, \ldots, N\). We evaluate \(f(L)\) for a fixed \(L\) with Dynamic Programming (DP; specifically, so-called “digit DP”). Consider the process of deciding the most significant bits of the entire \(L\) elements to the least, we fill the following DP table:

\(\mathrm{dp}[i][j] = \) (the number of ways to determine the \(i\) significant bits of all the \(L\) elements such that \(j\) elements are known to be strictly less than \(M\)).

When deciding the \((i+1)\)-th bit, \((L-j+1)\) transitions occurs from \(\mathrm{dp}[i][j]\), each corresponding to, among the \((L-j)\) elements that is not known to be strictly less than \(M\), how many are set to \(1\).

The time complexity of this DP for each \(L\) is \(O(L^2\log \max\lbrace M, X\rbrace)\), for a total over \(L\) of \(O(N^3\log \max\lbrace M, X\rbrace)\).

[2] Find the number of good sequences consisting of distinct integers

Let us find \(f(L)\), the number of good sequences of length \(L\) consisting of distinct integers, for all \(L = 0, 1, 2, \ldots, N\). For a fixed \(L\), we find \(g(L)\) by subtracting “the number of good sequences of length \(L\) containing duplicating elements” from \(f(L)\).

Let us call an integer peculiar if there are odd number of occurrences of the integer in \(A\); also, let us call an element of \(A\) peculiar if its value is a peculiar integer.

Since the total XOR of the sequence is that of peculiar integers in \(A\), we can find the number of good sequences of length \(L\) that may contain duplicating elements that have

  • \(i\) peculiar elements,
  • \(j\) peculiar integers, and
  • \(k\) non-peculiar integers

can be found as follows:

  • First, choose the positions of \(i\) peculiar elements out of \(L\) elements (\(\binom{L}{i}\) ways).
  • Divide the \(i\) peculiar elements into \(j\) group with odd sizes (let’s say there are \(\mathrm{odd}(i, j)\) ways), and assign distinct integers with total XOR \(X\) to each of the \(j\) groups (\(g(j)\) ways).
  • Divide \((L-i)\) non-peculiar elements into \(k\) indistinguishable groups (let’s say there are \(\mathrm{even}(L-i, k)\) ways), and assign distinct integers chosen from \((M+1-j)\) integers (i.e. integers between \(0\) and \(M\) except for the already chosen \(j\) integers) to each of the \(k\) groups (\(\prod_{l = M+1-j-k+1}^{M+1-j} l\) ways).

Therefore, there are

\[\binom{L}{i} \mathrm{odd}(i, j) g(j) \mathrm{even}(L-i, k) \prod_{l = M+1-j-k+1}^{M+1-j} l\]

ways to do so. Thus, in total, there are

\[\sum_{i = 0}^L \binom{L}{i} \sum_{j = 0}^{\min\lbrace L-1, i\rbrace} \mathrm{odd}(i, j) g(j) h(L-i, j)\]

good sequences consisting of distinct integers, where

\[h(x, y) \coloneqq \sum_{k= 0}^{x} \mathrm{even}(x, k) \prod_{l = M+1-y-k+1}^{M+1-y} l.\]

The necessary values of \(h(\cdot, \cdot)\) can be precalculated in a total of \(O(N^2)\) time. Also, \(\mathrm{odd}(\cdot, \cdot)\) and \(\mathrm{even}(\cdot, \cdot)\) can be found as

\[\mathrm{odd}(i, j) = \sum_{k:\text{odd}} \binom{i-1}{k-1} \mathrm{odd}(i-k, j-1)\]

and

\[\mathrm{even}(i, j) = \sum_{k:\text{even}} \binom{i-1}{k-1} \mathrm{even}(i-k, j-1)\]

in a total of \(O(N^2)\) time.

Hence, we can find \(g(L)\) in an \(O(N^2)\) time for each \(L\).

[3] Finding the answer to the original problem

A sequence satisfying the conditions in the Problem Statement are the sequences is obtained by adding even number of integers between \(0\) and \(M\) to a strictly ascending sequence of length at most \(N\). If we are to add \(2i\) integers, the number of sequences satisfying the conditions in the Problem Statement is the product of

  • the number of strictly-ascending good sequences of length \((N-2i)\): \(g(N-2i)/(N-2i)!\), and
  • the number of ways to choose \(2i\) integers between \(0\) and \(2M\), each of which is chosen even number of times: \(\binom{M+i}{i}\).

Therefore, the answer to this problem is

\[\sum_{i = 0}^{\lfloor \frac{N}{2} \rfloor} \frac{g(N-2i)}{(N-2i)!}\binom{M+i}{i}.\]

posted:
last update: