1382B.md (2530B)
1 Source: https://codeforces.com/problemset/problem/1382/B 2 3 B. Sequential Nim 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 There are $n$ piles of stones, where the $i$-th pile has $a_i$ stones. Two people play a game, where they take alternating turns removing stones. 22 23 In a move, a player may remove a positive number of stones from the first non-empty pile (the pile with the minimal index, that has at least one stone). The first player who cannot make a move (because all piles are empty) loses the game. If both players play optimally, determine the winner of the game. 24 25 Input 26 27 The first line contains a single integer $t$ ($1\le t\le 1000$) — the number of test cases. Next $2t$ lines contain descriptions of test cases. 28 29 The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) — the number of piles. 30 31 The second line of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\le a_i\le 10^9$) — $a_i$ is equal to the number of stones in the $i$-th pile. 32 33 It is guaranteed that the sum of $n$ for all test cases does not exceed $10^5$. 34 35 Output 36 37 For each test case, if the player who makes the first move will win, output "First". Otherwise, output "Second". 38 39 Example 40 41 Input 42 43 ``` 44 7 45 3 46 2 5 4 47 8 48 1 1 1 1 1 1 1 1 49 6 50 1 2 3 4 5 6 51 6 52 1 1 2 1 2 2 53 1 54 1000000000 55 5 56 1 2 2 1 1 57 3 58 1 1 1 59 ``` 60 61 Output 62 63 ``` 64 First 65 Second 66 Second 67 First 68 First 69 Second 70 First 71 ``` 72 73 Note 74 75 In the first test case, the first player will win the game. His winning strategy is: 76 77 1. The first player should take the stones from the first pile. He will take $1$ stone. The numbers of stones in piles will be $[1, 5, 4]$. 78 2. The second player should take the stones from the first pile. He will take $1$ stone because he can't take any other number of stones. The numbers of stones in piles will be $[0, 5, 4]$. 79 3. The first player should take the stones from the second pile because the first pile is empty. He will take $4$ stones. The numbers of stones in piles will be $[0, 1, 4]$. 80 4. The second player should take the stones from the second pile because the first pile is empty. He will take $1$ stone because he can't take any other number of stones. The numbers of stones in piles will be $[0, 0, 4]$. 81 5. The first player should take the stones from the third pile because the first and second piles are empty. He will take $4$ stones. The numbers of stones in piles will be $[0, 0, 0]$. 82 6. The second player will lose the game because all piles will be empty.