公式

E - Pass to Next 解説 by evima 印刷ページ


Let \(c_i\) be the number of balls that Person \(i\) hands to the neighbor to the right.

When \(\min(c) \neq 0\), decreasing every \(c_i\) by \(1\) would not change the resulting sequence, so we just need to consider the case \(\min(c) = 0\). Below, we assume \(\min(c) = 0\).

There are \(\prod_{i=1}^{N}(a_{i}+1) - \prod_{i=1}^{N}a_i\) such sequences \(c\), all of which result in distinct sequences. Thus, we are done if we can find the resulting sequence for every sequence \(c\) such that \(\min(c)=0\), and find the sum of their products.

The value \(\prod_{i=1}^{N}x_i\) is equal to the number of ways to choose one from each person’s balls after the procedure. To find the sum, we can use dynamic programming to find counts such as the number of ways to decide \(c_1, c_2, \ldots, c_i\) and choose one ball from each of Persons \(1, 2, 3, \ldots, i, i+1\) (note that we need to consider the case we have not chosen a ball from Person \(1\), Person \(i+1\), or both). This can be done in \(O(N)\) time, which is fast enough.

投稿日時:
最終更新: