This page is still under construction.

Parts of this page are still being built. What you see may change.

Cow Digit Game

Interview

Time limit1sMemory limit128 MB

Summary
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 NN (1≤N≤1,000,0001 \le N \le 1{,}000{,}000). 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 30143014 a player may subtract either 11 or 44, reaching 30133013 or 30103010. Play continues until the number becomes 00; the player who makes the last move (the one who reaches 00) wins.

Bessie and Farmer John play GG games (1≤G≤1001 \le G \le 100). 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 N=13N = 13. Bessie moves first and subtracts 33, leaving 1010. Farmer John is forced to subtract 11, leaving 99. Bessie subtracts 99, reaching 00, and wins.

Input

  • Line 11: a single integer GG.
  • Lines 2…G+12 \ldots G+1: line i+1i+1 contains a single integer NiN_i, the starting number of game ii.

Output

  • Lines 1…G1 \ldots G: line ii contains "YES" if Bessie wins game ii, and "NO" otherwise.

Hint

With the starting number 99, Bessie subtracts 99 and wins immediately. With the starting number 1010, Bessie must subtract 11 (she cannot subtract 00), leaving 99; Farmer John then subtracts 99 and wins, so Bessie loses.

Examples4

  1. Example 1

    Input
    2
    9
    10
    
    Expected output
    YES
    NO
    
  2. Example 2

    Input
    1
    1
    
    Expected output
    YES
    
  3. Example 3

    Input
    1
    13
    
    Expected output
    YES
    
  4. Example 4

    Input
    9
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    Expected output
    YES
    YES
    YES
    YES
    YES
    YES
    YES
    YES
    YES