公式

G - Completely Wrong 解説 by en_translator


Let \(S_i\) be the set of permutations \(P\) of \((1,2,\ldots,N)\) with \(C_{P_i}=G_i\). Then the sought value is \(\displaystyle \frac1{N!}\left|\bigcap_{i=1}^N \bar{S_i} \right|\), which equals \(\displaystyle \frac1{N!}\sum_{A \subset \lbrace 1,2,\ldots,N\rbrace} (-1)^{|A|}\left|\bigcap_{i \in A}S_i \right|\) the principle of inclusion-exclusion. Thus, it is sufficient to find, when a fixed index set satisfies \(C_{P_i}=G_i\), the number of ways to determine the values for the remaining indices, multiplied by \((-1)^{|A|}\) and summed up.

Let \(X_k,Y_k\) be the number of indices \(i\) with \(C_i,G_i=k\), respectively.

For each \(i\), fix the number \(D_k\) of elements \(k \in A,\ G_i=k\). There are \(\displaystyle \prod_{k=1}^N \binom{X_k}{D_k}\binom{Y_k}{D_k}D_k!\) sets eligible for \(A\). For such \(A\), \(\displaystyle \left|\bigcap_{i \in A}S_i \right|=(N-|A|)!\), so the sought count equals \(\displaystyle \frac1{N!}\sum_{0\le D_k\le C_k\ (1\le k\le N)} (-1)^{\sum_k D_k} \prod_{k=1}^N \binom{X_k}{D_k}\binom{Y_k}{D_k}D_k!\left(N-\sum_{k} D_k\right)!\).

This in turn equals to \(\displaystyle \frac1{N!}\sum_{i\geq 0}c_i(N-i)!\), where \(\displaystyle f_k(x)=\sum_{j\geq 0} (-1)^j\binom{X_k}{j}\binom{Y_k}{j}c!x^j,\ f(x)=\prod_{k=1}^N f_k(x)=\sum_{i\geq 0}c_ix^i\).

This \(f(x)\) can be obtained as the product of polynomials whose degrees sum to \(N\), which can be computed in a total of \(O(N\log^2N)\) time by incrementally merging two polynomials with the smallest degrees.

The problem can be solved by properly implementing this algorithm.

投稿日時:
最終更新: