2107B.md (2085B)
1 Source: https://codeforces.com/problemset/problem/2107/B 2 3 B. Apples in Boxes 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Tom and Jerry found some apples in the basement. They decided to play a game to get some apples. 22 23 There are $n$ boxes, and the $i$-th box has $a_i$ apples inside. Tom and Jerry take turns picking up apples. Tom goes first. On their turn, they have to do the following: 24 25 * Choose a box $i$ ($1 \le i \le n$) with a positive number of apples, i.e. $a_i \gt 0$, and pick $1$ apple from this box. Note that this reduces $a_i$ by $1$. 26 * If no valid box exists, the current player loses. 27 * If after the move, $\max(a_1, a_2, \ldots, a_n) - \min(a_1, a_2, \ldots, a_n) \gt k$ holds, then the current player (who made the last move) also loses. 28 29 If both players play optimally, predict the winner of the game. 30 31 Input 32 33 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. 34 35 The first line of each test case contains two integers $n,k$ ($2 \le n \le 10^5,1\le k \le 10^9$). 36 37 The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). 38 39 It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. 40 41 Output 42 43 For each test case, print "Tom" (without quotes) if Tom will win, or "Jerry" (without quotes) otherwise. 44 45 Example 46 47 Input 48 49 ``` 50 3 51 52 3 1 53 54 2 1 2 55 56 3 1 57 58 1 1 3 59 60 2 1 61 62 1 4 63 ``` 64 65 Output 66 67 ``` 68 Tom 69 Tom 70 Jerry 71 ``` 72 73 Note 74 75 Note that neither player is necessarily playing an optimal strategy in the following games, just to give you an idea of how the game is going. 76 77 In the first test case of the example, one possible situation is shown as follows. 78 79 * Tom takes an apple from the first box. The array $a$ becomes $[1, 1, 2]$. Tom does not lose because $\max(1, 1, 2) - \min(1, 1, 2) = 1 \le k$. 80 * Jerry takes an apple from the first box as well. The array $a$ becomes $[0, 1, 2]$. Jerry loses because $\max(0, 1, 2) - \min(0, 1, 2) = 2 \gt k$.