Marbles
InterviewTime limit2sMemory limit512 MB
Treat marbles as Wythoff/Nim heap coordinates and compute Sprague-Grundy values to decide if the first player wins.
- Level
Medium6 of 10
- Topics
- Game theory, Math, Simulation, Implementation
- Solved
- No attempts yet
Problem
Using marbles as currency did not work out well in Cubicônia. Wanting to make amends with his friends after stealing their marbles, the emperor decided to invite everyone to an evening of games at his palace.
Naturally, the games use marbles, since the emperor now needs to find a use for so many of them. N marbles are spread over a large board whose rows are numbered 0 to L and whose columns are numbered 0 to C. The players take turns, and on each turn the player whose turn it is must choose one of the marbles and move it. The first player to move a marble to position (0, 0) wins. To keep the game interesting, moves are restricted; otherwise the first player would always move a marble to position (0, 0) and win. A move consists of choosing an integer u greater than 0 and a marble, whose location we denote by (l, c), and moving it to one of the following positions, provided the position stays on the board:
- (l − u, c);
- (l, c − u); or
- (l − u, c − u).
Note that more than one marble may occupy the same position on the board.
Since the emperor does not like to lose, you must help him determine which games he should take part in. As expected, the emperor always has the first turn when he plays. Assuming everyone plays optimally, your program must analyze the initial distribution of the marbles on the board and report whether it is possible for the emperor to win if he plays.
Input
The first line contains an integer N (1 ≤ N ≤ 1000). Each of the following N lines contains two integers li and ci, indicating the row and column where the i-th marble is located on the board (1 ≤ li, ci ≤ 100).
Output
Your program must print a single line containing the character Y if it is possible for the emperor to win the game, or N otherwise.