Official

E - Doubling Editorial by evima


The problem is to count integer sequences \((c_0,c_1,c_2,\cdots,c_N)\) satisfying the following conditions.

  • \(c_0=0\)
  • \(0 \leq c_i \leq 2 \times c_{i-1} + X_i\)
  • \(\sum c_i = k\)

A natural approach is to compute the generating function \(F_i(z) = \sum_{c_1,\cdots,c_i} z^{c_1+\cdots+c_i}\) for the sum of the first \(i\) terms.

Let us expand \(F_i(z)\). If we fix the first \(i-1\) terms, the \(i\)-th term can be chosen in the range \(0 \leq c_i \leq 2 \times c_{i-1} + X_i\), so the following holds.

\[ F_i(z)=\sum _{c_1,\cdots,c _{i-1}} \frac{1-z^{2 \times c _{i-1} + X_i+1}}{1-z} z^{c_1+\cdots+c _{i-1}} \]

Rearranging this gives the following.

\[ F_i(z)=\frac{1}{1-z} \left( F _{i-1}(z) - z^{X _i+1} \sum _{c_1,\cdots,c _{i-1}} z^{c _1+\cdots+c _{i-2}+ 3 \times c _{i-1}} \right) \]

Looking at this, we also want to compute the generating function \(G_i(z)=\sum _{c_1,\cdots,c_i} z^{c _1+\cdots+c _{i-1}+ 3 \times c _i}\). If we try to compute it in the same way as \(F_i(z)\), we now need \(\sum _{c_1,\cdots,c_i} z^{c _1+\cdots+c _{i-1}+ 7 \times c _i}\).

Continuing in this way, we see that it suffices to compute \(F_{i,j}(z) = \sum _{c_1,\cdots,c_i} z^{c _1+\cdots+c _{i-1}+ (2^j-1) \times c _i}\) for each \(j=1,2,\cdots\).

Now, each \(F_{i,j}(z)\) only needs to be computed up to degree \(K\). From this, the required range of \(j\) is those satisfying \(2^j-1 \leq K\).

\(F_{i,j}(z)\) can be computed directly using the formula above. Thus, the problem can be solved in \(O(NK \log K)\) time in total.

Sample Solution (C++)