Cow Digit Game
InterviewTime limit1sMemory limit128 MB
For each starting number, players alternately subtract its largest or smallest nonzero digit, and the player who reaches 0 wins; decide if the first player wins.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Game theory, Math, Implementation
- Solved
- No attempts yet
Problem
Bessie is playing a number game against Farmer John, and she wants you to help her win.
Each game starts with an integer (). Bessie moves first, and the two players then alternate turns. On each turn, the current player subtracts either the largest digit or the smallest non-zero digit of the current number from that number. For example, from a player may subtract either or , reaching or . Play continues until the number becomes ; the player who makes the last move (the one who reaches ) wins.
Bessie and Farmer John play games (). For each game, decide whether Bessie or Farmer John wins, assuming both play optimally: on every turn a player who has a move that guarantees a win will make such a move.
As an illustration, suppose . Bessie moves first and subtracts , leaving . Farmer John is forced to subtract , leaving . Bessie subtracts , reaching , and wins.
Input
- Line : a single integer .
- Lines : line contains a single integer , the starting number of game .
Output
- Lines : line contains "YES" if Bessie wins game , and "NO" otherwise.
Hint
With the starting number , Bessie subtracts and wins immediately. With the starting number , Bessie must subtract (she cannot subtract ), leaving ; Farmer John then subtracts and wins, so Bessie loses.