#P3235. Turn‐Based Stone Splitting Game
Turn‐Based Stone Splitting Game
Turn‐Based Stone Splitting Game
You are given a turn‐based stone splitting game. The game is defined as follows. At the beginning a parameter \(F\) is given. In each game (there are \(T\) games in total) there are \(N\) piles of stones. Two players take turns. On a turn, the current player chooses any pile with at least \(F\) stones and then picks an integer \(M\) (with \(M \ge 2\), and it may be different on each move). The chosen pile with \(X\) stones is then split into \(M\) piles in the only possible way that the resulting piles are as equal as possible; more precisely, if \(q=\lfloor X/M \rfloor\) and \(r=X\bmod M\), then the pile is split into \(r\) piles of \(q+1\) stones and \(M-r\) piles of \(q\) stones. (Note that the difference between the pile with the most stones and the pile with the fewest stones is at most 1.)
A player loses if, at the beginning of his turn, every pile has fewer than \(F\) stones, meaning he cannot make any move. It is given that the first move is made by your opponent. Assuming both players play optimally, determine who will win the game. Output First
if the first (who is your opponent) wins, or Second
if you (the second player) win.
Game Note: When a move is made by splitting a pile, the total number of piles increases by \(M-1\) (since one pile is replaced by \(M\) piles). The only allowed move is to select a pile with at least \(F\) stones and to split it as described.
The key to solving this problem is to observe that it is an impartial game. For any pile of size \(X\), if \(X < F\) no move is possible, so its Grundy value is 0. For \(X \ge F\), every valid move (choosing some \(M\) with \(2 \le M \le X\)) transforms the pile into a fixed multiset of piles with sizes determined by the formula above. Let \(G(X)\) denote the Grundy value of a pile of size \(X\). Then
$$G(X)=\begin{cases}0,&X<F,\\ \text{mex}\{G(q+1)\oplus \cdots \oplus G(q)\},&X\ge F,\end{cases} $$where for a move with a chosen \(M\), if \(q=\lfloor X/M \rfloor\) and \(r=X\bmod M\), the move produces \(r\) copies of a pile with \(q+1\) stones and \(M-r\) copies of a pile with \(q\) stones, and \(\oplus\) denotes bitwise XOR. The overall game is the disjunctive sum of the piles, so the outcome is determined by the nim-sum of the Grundy values of the individual piles.
Given the initial configuration of stones in each game, decide the winner assuming optimal play. Remember, the opponent makes the first move.
inputFormat
The first line contains a single integer \(T\) (the number of games). For each game, the first line contains two integers \(N\) and \(F\) where \(N\) is the number of piles and \(F\) is the threshold for making a move. The second line contains \(N\) positive integers representing the sizes of the piles.
outputFormat
For each game, output a single line containing either First
if the first player (your opponent) wins under optimal play, or Second
if the second player wins.
sample
3
3 3
3 4 5
4 2
2 3 4 5
2 3
3 3
First
First
Second
</p>