Palindrome Game
Time limit1sMemory limit1024 MB
With a pile of N stones, players alternately remove a palindromic number of stones; determine whether the first player wins.
- Level
Hard8 of 10
- Topics
- Game theory, Dynamic programming, Math, Implementation
- Solved
- No attempts yet
Problem
Sangyoon and Seungwoo are fierce rivals with a record of 500 wins and 500 losses in 1000 games.
To settle who is better at games, they decide to play one final game.
The rules are as follows.
- The game is played with a pile of stones.
- Sangyoon moves first. After that, Seungwoo and Sangyoon alternate turns.
- On your turn, you may take stones from the pile. Here must be a palindrome number.
- If you have no stones to take on your turn, you lose.
A palindrome number is a positive integer that reads the same when reversed, such as , , and . Numbers with leading zeros, such as , do not count.
Both Sangyoon and Seungwoo have played 1000 games and always make the best move.
Given the number of stones , print who wins.
Input
The first line gives the number of test cases . ()
Each of the next lines gives the number of stones in the pile. ()
Output
For each game, print 0 if Sangyoon wins and 1 if Seungwoo wins.