#P3339. Generalized Take-Away Stone Game
Generalized Take-Away Stone Game
Generalized Take-Away Stone Game
This problem is a generalization of the classical stone removal game. Two players are given a list of nonnegative integers written on paper, and a constant K (a positive integer) is determined before the game. The players move alternately. In each move, the player chooses one number x from the list. If x < K, the chosen number cannot be used. Otherwise, the player must choose a positive integer a subject to the condition \[ x - K \times a \ge 0 \] After choosing such an a, the chosen number x is erased and K new numbers are written: \[ x-a,\quad x-2a,\quad \dots, \quad x-K\times a \quad. \] If a player cannot make any move (i.e. none of the available numbers is at least K), then that player loses and the other wins. Both players are assumed to play optimally.
Your task is to determine, for a given initial configuration and a given constant K, whether the first player (the player who makes the first move) has a winning strategy.
Game Theory Background: By using the Sprague-Grundy theorem, we can assign a Grundy number (a nimber) to each number in the list. The game position is winning for the first player if the XOR of the Grundy numbers of all piles is non-zero.
How to compute the Grundy number:
For a number x, if x < K then no move can be made and the Grundy number is 0. For x ≥ K, consider every valid move. For every positive integer a satisfying x - K*a \ge 0, the move creates K numbers: x-a, x-2a, ..., x-K*a. The Grundy number for x is the minimum excludant (mex) of all XOR sums of the Grundy numbers of these K numbers.
Decision: Compute the XOR of the Grundy numbers for all given numbers. If the XOR is non-zero, the answer is "First"; otherwise it is "Second".
inputFormat
The input begins with a line containing two integers N and K (1 ≤ N ≤ 1000, 1 ≤ K ≤ 100), where N is the number of numbers on the paper and K is the constant described in the rules. The next line contains N nonnegative integers (each not exceeding 104) representing the initial numbers.
outputFormat
Output a single word: "First" if the first player has a winning strategy, or "Second" otherwise.
sample
3 2
2 5 4
First
</p>