#P11077. Stone Pile Game
Stone Pile Game
Stone Pile Game
You are given n piles of stones, where the i-th pile contains ai stones. Let \( x \) be the average of the sequence \( a_1, a_2, \cdots, a_n \), i.e., \( x = \frac{a_1+a_2+\cdots+a_n}{n} \). You are also given an integer k such that \( k \le x \). Two players, F and L, play the following game alternately (F moves first):
- Select two piles \( i \) and \( j \) such that \( a_i < x < a_j \). If no such pair of piles exists, then the current player loses and the opponent wins.
- Remove exactly \( k \) stones from pile \( j \) (which satisfies \( a_j > x \)) and add them to pile \( i \) (which satisfies \( a_i < x \)).
Both players play optimally. The game stops when a player is unable to make a move. In addition, if the game continues indefinitely, output Draw
.
Your task is to determine the result of each game. For each game, output:
Draw
if the game will continue forever.F
if player F (the first mover) wins when the game terminates.L
otherwise.
Game Analysis
Since moving \( k \) stones from one pile to another keeps the total number of stones (and thus the average \( x \)) invariant, the only possible moves are those that transfer surplus from piles having more than \( x \) to those having fewer than \( x \). A move is only valid if:
- There exists a pile with \( a_i x \).
- After a sequence of moves, a pile will reach \( x \) only if the difference \( |a_i - x| \) is divisible by \( k \). Otherwise, the pile will never reach the balanced state, and the game will continue indefinitely.
If \( x \) is not an integer (i.e. \( \sum_{i=1}^{n} a_i \) is not divisible by \( n \)), then some piles will always be strictly less than or greater than \( x \), and the game will never terminate (i.e. the outcome is Draw
).
Assume now that \( x \) is an integer and that for every pile, \( |a_i - x| \) is divisible by \( k \). Let the number of moves needed to bring all piles to \( x \) be given by:
[ m = \sum_{a_i < x} \frac{x - a_i}{k} = \sum_{a_j > x} \frac{a_j - x}{k}. ]
The players alternate moves. If no move is possible at the start, then player F loses. Otherwise, if \( m \) is odd, then player F makes the last move and wins; if \( m \) is even, then player L wins.
inputFormat
The first line contains a single integer T
(1 ≤ T ≤ 104), the number of games.
Each game is described as follows:
- The first line contains two integers
n
andk
(2 ≤ n ≤ 105 andk
is a positive integer with \( k \le x \)). - The second line contains
n
integers:a1, a2, ..., an
(1 ≤ ai ≤ 109).
The total sum of n
over all test cases does not exceed 105.
outputFormat
For each game, output a single line containing one of the following:
Draw
— if the game continues indefinitely.F
— if the first player wins.L
— if the second player wins.
sample
3
2 1
1 3
2 1
1 5
3 2
1 2 3
F
L
Draw
</p>