The divisibility game
Time limit2sMemory limit512 MB
Two players alternately delete numbers from a set; decide whether X can force the remaining sum to be divisible by P after exactly K deletions.
- Level
Hard8 of 10
- Topics
- Game theory, Combinatorics, Math
- Solved
- No attempts yet
Problem
Two players, X and Y, play the following game.
- They are given a positive integer and a set of distinct nonnegative integers. Every is less than .
- The players take turns alternately. On each turn, the player to move deletes one number from .
- After exactly turns, if the sum of the numbers remaining in is divisible by , X wins. Otherwise, Y wins.
Write a program that determines who wins when both players play optimally.
Input
The first line contains a positive integer , the number of games in the input.
Then, for each :
- line contains , , and , separated by spaces;
- line contains a single character,
XorY, which is the player who moves first; - line contains , separated by spaces.
Output
Print one line with characters and no separators, one character per game. The -th character is X if X wins the -th game no matter how Y plays, and Y otherwise.
Limits
- for every , and for every