Custom Stone Taking

2 secs 1024 MB
magurofly's icon magurofly

この問題は、 DP を累積和で高速化することによって解くことができます。

まず、ゲーム的な DP (dp[n]=\text{dp}[n] = 石が nn 個残っているときに今の手番の人が勝てるかどうか)を考えると、次のようになります。このとき、 dp[N]\text{dp}[N]true\text{true} なら高橋くんの勝ち、そうでなければ青木くんの勝ちです。(なお、 \wedge は AND 、 xˉ\bar{x} は NOT の意味です。)

dp[0]=falsedp[n]=i=AnBndp[ni]\begin{aligned} \text{dp}[0] &= \text{false} \\ \text{dp}[n] &= \overline{\bigwedge_{i = A_n}^{B_n} \text{dp}[n - i]} \end{aligned}

以上の二番目の式は、 dp[nBi]dp[n - B_i] から dp[nAi]dp[n - A_i] までに false\text{false} があれば true\text{true} 、そうでなければ false\text{false} となります。よって、 false\text{false} の個数の累積和を取ると、次のようにできます。(Σdp[i]\text{Σdp}[i]dp[0]\text{dp}[0] から dp[i]\text{dp}[i] までにある false\text{false} の個数です。)

Σdp[0]=1Σdp[n]=Σdp[n1]+{0Σdp[nAi]Σdp[nBi1]11otherwise\begin{aligned} \text{Σdp}[0] &= 1 \\ \text{Σdp}[n] &= \text{Σdp}[n - 1] + \begin{cases} 0 & \text{Σdp}[n - A_i] - \text{Σdp}[n - B_i - 1] \ge 1 \\ 1 & \text{otherwise} \end{cases} \end{aligned}

このようにすると、 Σdp[N]Σdp[N1]=0\text{Σdp}[N] - \text{Σdp}[N - 1] = 0 なら高橋くんの勝ちということになります。