We need to analyze this impartial combinatorial game? Actually it's a two-player deterministic perfect information game with alternating moves. The state is a multiset of positive integers (initially A_i). The allowed move: choose an integer >=2, subtract 1 from it (so that integer becomes at least 1). Then compute g = gcd of all numbers on the board after subtraction, and divide all numbers by g. So the operation reduces numbers but also normalizes by gcd.
Goal: The player who cannot move loses. The terminal position is when all numbers are 1s, because then no integer >=2 exists. So it's a normal play impartial game? However the move is not exactly symmetric? But both players have same moves, so it's impartial. However the state is defined by the multiset. Since after division by gcd, the numbers become coprime as a whole (gcd=1). Actually after division, gcd becomes 1 by definition. So after each move, the gcd of the numbers is 1. Also before move, gcd may be >1? Initially gcd is given as 1. After a move, we subtract 1 from some number, then recompute gcd and divide. It's plausible that during the game the gcd may become >1 before division, but we always divide to make gcd=1. So after each move, the state is a multiset with gcd 1.
Observation: The move can be seen as a combination of decrement and then normalization. The normalization by dividing by gcd shrinks numbers drastically sometimes. Equivalent to: given a multiset with gcd=1, a move is: pick i such that a_i >=2, reduce a_i by 1 to get a_i' = a_i - 1, then compute g = gcd(all a_j' for j≠i, and a_i'), then replace each a_j' by a_j'/g. This yields a new multiset with gcd=1.
Goal: Determine winner under optimal play.
We need to find some invariant or characterization.
First, note that the operation of dividing by gcd is like removing common factors. Since after subtraction, numbers might share a common factor, we divide it out. This reduces the total sum significantly.
We can think of the game in terms of the numbers in their prime factorizations? Perhaps we can relate to the classic Euclid's algorithm or "Turning Turtles"? There is known game: "Subtract a square" etc. Not.
Alternate perspective: Consider the state as a multiset of integers with gcd=1. The move: decrease one number by 1, then divide all numbers by their common gcd. That is reminiscent of the game "Chomp"? No.
Maybe we can transform to a Nim-like game. Let's consider the effect on the sum of numbers. Let S = sum of numbers. After subtracting 1, sum decreases by 1. Then dividing by g, sum becomes (S - 1)/g. Since g >= 1, the sum reduces by factor at least. However g can be 1. If g=1, then the state is just the decremented numbers (no division). So only when after decrement the gcd becomes >1, we divide and reduce numbers.
Observation: If after decrement, the gcd becomes some g>1, then dividing reduces the numbers making them smaller. That seems beneficial for the player who moves? Actually dividing reduces numbers, which could bring the game closer to terminal position. So making gcd>1 may speed up the end.
Key: The terminal position is all ones. So essentially, the game ends when all numbers become 1.
Think about parity? Maybe the outcome depends on the sum of (a_i - 1) or something.
Let's try small examples to detect pattern.
Example1: 3,6,7 => output First.
Example2: 1,2,4,8 => First.
Example3: 7,8,8,8,8 => Second.
Try to analyze from base cases.
If all numbers are 1: no moves, losing position.
If exactly one number is >1 and others are 1. Suppose state: [k, 1,1,...,1] with gcd=1 implies k must be 1? Actually if k>1 and all others 1, gcd of (k,1,...,1) =1 because 1 and k always gcd1. So state is valid. What moves? Choose the only number >=2, subtract 1: then we have [k-1,1,...1]. Then compute g = gcd(k-1,1,...1) = gcd(k-1,1) =1 (since 1 present). So division by 1 does nothing. So the move reduces that number by 1. So it's just a countdown: each move decreases that number by 1 until it becomes 1. So number of moves = k-1. Starting player? If k-1 is odd, first player makes last move? Actually after last move, number becomes 1, all ones, next player cannot move. So the player who makes the last move wins (since they leave terminal for opponent). So if number of moves is odd, first player wins; if even, second wins. So for state with only one >1 number k, the outcome is First if (k-1) is odd (i.e., k even?) Actually k-1 odd means k is even? Let's check: if k=2, moves=1 (odd) -> First wins. If k=3, moves=2 (even) -> Second wins. So simple.
Now consider two numbers >1? Let's analyze small states manually.
Simplify: Because division by gcd normalizes, we can think of the numbers as being coprime (gcd=1). After a move, we always divide by gcd to ensure the new state also has gcd=1. So we can always assume state's gcd =1.
Define state as multiset of positive integers with gcd=1.
Consider effect of subtracting 1 from a number a_i. Let a_i' = a_i-1. Then compute g = gcd(all numbers after decrement). Since before decrement gcd=1, it's possible that after decrement, gcd becomes >1. Division yields new numbers b_j = a_j' / g (with a_j' for all j). Note that after division, gcd(b) =1.
Observation: If we consider the multiset of numbers in terms of their prime factors, dividing by g removes common factor.
Maybe we can transform the game to a different representation. Note that after division, the numbers are typically smaller. Could it be that the game is equivalent to playing on the set of numbers in terms of their "odd part" etc?
Another angle: Consider the total number of moves? Possibly the game outcome is determined by the parity of sum of (a_i - 1) after some reductions? But example2: [1,2,4,8] sum=15, sum-1's = (0+1+3+7)=11 odd? 11 odd => First? Actually first wins. Example1: [3,6,7] sum-1's =2+5+6=13 odd => First. Example3: [7,8,8,8,8] sum-1's=6+7+7+7+7=34 even => Second. Could be that the parity of (sum - N) determines outcome? sum - N = total excess over 1. For ex1: 16-3=13 odd; ex2: 15-4=11 odd; ex3: 39-5=34 even. So indeed matches: First if (sum - N) odd, Second if even? Let's test other simple cases.
If single number k: sum - N = k - 1, which is odd => First wins if k even? Wait k-1 odd => k even gives first? Actually for k=2, k-1=1 odd => First wins (correct). For k=3, k-1=2 even => Second wins (correct). So matches.
But is that always true? Let's test some other small states.
Consider state [2,2]. N=2, sum=4, sum-N=2 even => Second should win. Let's simulate manually.
State: (2,2) gcd=2? But initial gcd must be 1. Actually [2,2] gcd=2, not allowed. Wait condition says greatest common divisor of integers from A_1 through A_N is 1. So state [2,2] not allowed initially. However after moves, we always divide by gcd, so state will have gcd=1. So state with two numbers both even cannot appear because their gcd would be at least 2 unless we allow dividing? Actually after each move, we divide by gcd, so the state always has gcd=1. So [2,2] cannot occur. So we need to consider states with gcd=1. Eg: [2,3] sum=5 sum-N=3 odd => First.
Simulate [2,3] (gcd=1). Moves: Player can subtract 1 from 2 or 3.
Case: subtract from 2: new numbers (1,3). gcd(1,3)=1, no division. State becomes (1,3). This is like a single >1 number 3. From earlier, (1,3) has moves left = 2 (since 3->2->1? Actually need to analyze: state (1,3) is terminal? Not yet, since 3>=2. It is a state with one >1. The player to move at (1,3) will win if (3-1) odd? (3-1)=2 even, so position (1,3) is losing for player to move? Let's compute: (1,3): move: subtract 1 from 3 -> (1,2). Then (1,2): move: subtract 1 from 2 -> (1,1). So from (1,3) the player moves to (1,2). Then opponent moves to (1,1) and wins? Actually after opponent moves to (1,1), the next player (original) cannot move and loses. So the player who moves from (1,2) wins. So (1,2) is winning for player to move. And (1,3) leads to (1,2) which is winning for opponent, so (1,3) is losing for player to move. So (1,3) is a losing position. So from (2,3) if first player subtracts from 2, resulting state (1,3) is losing for opponent, thus winning for first player. So there is a winning move. So (2,3) is winning (First). That matches sum-N=3 odd.
Now test [2,5] sum=7 sum-N=5 odd => First. Simulate? Might be winning.
But we need a counterexample to test parity conjecture. Let's try state [3,4] (gcd=1). sum=7, N=2, sum-N=5 odd => First. Let's simulate to see if indeed winning.
State (3,4). Moves:
Option 1: subtract 1 from 3: new (2,4). gcd(2,4)=2, then divide by 2 -> (1,2). So resulting state (1,2). (1,2) we determined is winning for player to move (since 2-1=1 odd). So (1,2) is winning for the player who gets to move. That means from (3,4) if we go to (1,2), we give opponent a winning position, so that move is losing. Option 2: subtract 1 from 4: new (3,3). gcd(3,3)=3, divide by 3 -> (1,1). That's terminal, so opponent cannot move, so we win. So there is a winning move (subtract from 4 leads to terminal). So (3,4) is winning. So matches.
Now try [3,5] sum=8 sum-N=6 even => Second if conjecture. Let's simulate to see if it's losing for first player.
State (3,5) gcd=1. Moves:
- Subtract from 3: new (2,5). gcd(2,5)=1, so state (2,5). Is (2,5) winning or losing? We'll need to evaluate.
- Subtract from 5: new (3,4). This leads to (1,1) via subtract from 4? Actually careful: subtract 1 from 5: (3,4). gcd(3,4)=1? Actually 3 and 4 are coprime, so no division. So state becomes (3,4). We just determined (3,4) is winning for player to move. So if first player moves to (3,4), opponent gets a winning position, so that move is losing.
So first player's potential winning move would be if either (2,5) is losing. So we need to evaluate (2,5). Evaluate (2,5): moves: subtract from 2 -> (1,5) -> (since gcd=1) state (1,5). (1,5) is one >1: moves left = 4 (5-1) which is even, so (1,5) is losing for player to move? Wait: (1,5) has 4 moves left? Actually from (1,5) the player can only decrement the 5, each move reduces it by 1, until 1. So number of moves = 4, which is even, so the position (1,5) is losing for player to move because after even number of moves, opponent makes last move? Let's compute carefully: (1,5) -> (1,4) -> (1,3) -> (1,2) -> (1,1). Starting from (1,5), if it's player A's turn, then after 4 moves the game ends. If total moves left is even, then the player who moves from (1,5) will make move 1, opponent move 2, A move 3, opponent move 4, and after opponent's move 4, the board becomes (1,1), and it's A's turn, but cannot move, so A loses. So (1,5) is losing for player to move. So (1,5) is losing. So from (2,5), subtract from 2 leads to (1,5) which is losing for opponent, so that move is winning for the mover. That means (2,5) is winning (since there exists a winning move). So (2,5) is winning. Then back to (3,5): subtract from 3 leads to (2,5) which is winning for opponent, so that move is losing. The other move to (3,4) is also losing. So both moves lead to winning positions for opponent, thus (3,5) is losing for first player. So Second wins. That matches sum-N even. Good.
Now try more complex state: [2,3,6]? But gcd(2,3,6)=1? 6 shares 2 and 3, gcd = 1? Actually gcd(2,3,6) = 1 because 2 and 3 are coprime. Yes. sum=11, N=3, sum-N=8 even => Second. Let's test manually perhaps.
State: (2,3,6). Moves possible. Let's analyze via recursion maybe small enough.
List possible moves:
-
Subtract 1 from 2: new (1,3,6). Compute gcd(1,3,6) = 1, so state (1,3,6). This is a state with two numbers >1: 3 and 6. But note gcd(3,6)=3? However there is also 1, so overall gcd=1. So (1,3,6).
-
Subtract 1 from 3: new (2,2,6). Compute gcd(2,2,6) = 2. Then divide by 2 -> (1,1,3). So state (1,1,3). That's single >1=3.
-
Subtract 1 from 6: new (2,3,5). gcd(2,3,5)=1. So state (2,3,5).
We need to determine outcome of each resulting state.
First compute terminal statuses: (1,1,1) terminal losing. (1,1,3) is single >1=3. As earlier, single >1: moves left = 2 (since 3 to 1 requires two moves) even -> losing for player to move. So (1,1,3) is losing (i.e., a P-position, previous player wins). So if first player moves to (1,1,3), opponent gets losing position, so that move is winning.
Now evaluate (1,3,6). We need to compute its outcome.
State (1,3,6). Moves:
-
Subtract from 3: new (1,2,6). gcd(1,2,6) = 1? gcd(2,6)=2, but with 1 present => gcd=1. So state (1,2,6).
-
Subtract from 6: new (1,3,5). gcd=1, state (1,3,5).
-
Subtract from 2? Actually there is no 2; we have 1,3,6. So only 3 and 6.
Now evaluate (1,2,6). Moves:
a) Subtract from 2: (1,1,6). gcd(1,1,6)=1 -> (1,1,6). That's single >1=6. 6-1=5 moves? Actually moves left = 5 (since 6->5->4->3->2->1: 5 steps) odd? 5 odd -> winning for player to move? Let's compute: single >1: if number is k, then position is losing for player to move if (k-1) even, winning if odd. Since 6-1=5 odd, (1,1,6) is winning for player to move. So if we move to that, opponent gets winning position => that move is losing.
b) Subtract from 6: (1,2,5). gcd=1. Need to evaluate (1,2,5).
Also could subtract from 1? No, must be >=2.
Evaluate (1,2,5). Moves:
-
Subtract from 2: (1,1,5) -> single >1=5, 5-1=4 even -> losing for player to move. So if we move to (1,1,5), opponent gets losing -> winning move.
-
Subtract from 5: (1,2,4). gcd(1,2,4) = 1? gcd(2,4)=2, but 1 present ->1. So (1,2,4).
Now (1,2,4). Moves:
-
Subtract from 2: (1,1,4) -> single 4, 4-1=3 odd -> winning for player to move (i.e., opponent would get winning) => losing move.
-
Subtract from 4: (1,2,3). gcd=1. (1,2,3)
-
Could subtract from 1? No.
(1,2,3). Moves:
-
Subtract from 2: (1,1,3) -> single 3, 3-1=2 even -> losing for player to move => winning move for mover.
-
Subtract from 3: (1,2,2). But careful: subtract 1 from 3 gives (1,2,2). gcd(1,2,2) = 1? gcd(2,2)=2, but 1 present -> gcd=1. So state (1,2,2). (1,2,2) has two 2's and a 1. But gcd(1,2,2)=1, ok.
Now (1,2,2). Moves:
-
Subtract from a 2: Suppose subtract from first 2 -> (1,1,2). gcd(1,1,2)=1 -> (1,1,2). single 2, 2-1=1 odd -> winning for player to move? Actually single >1=2: moves left=1 odd => position (1,1,2) is winning for player to move (since they can move to (1,1,1) and opponent loses). So if we move to (1,1,2), opponent gets winning => losing move.
-
Subtract from the other 2 same.
-
Subtract from 1 not allowed.
So both moves from (1,2,2) lead to winning for opponent (since (1,1,2) is winning for the player whose turn). So (1,2,2) is losing (no winning move). But wait, check if any other move? No. So (1,2,2) is losing.
Now back to (1,2,3): we had move to (1,1,3) which is losing for player to move => that move is winning. So (1,2,3) has a winning move, thus (1,2,3) is winning.
Now (1,2,4): we had moves: to (1,1,4) losing? Actually we determined (1,1,4) is winning for player to move (since 4-1=3 odd), so giving opponent a winning position makes that move losing. The other move to (1,2,3). (1,2,3) we just found is winning. So both moves from (1,2,4) give opponent winning positions? Wait, if we move to (1,2,3), opponent gets a winning position, so that move is losing. If we move to (1,1,4), opponent gets winning position, also losing. Thus (1,2,4) appears to have no winning move, so (1,2,4) is losing. But we need to double-check (1,1,4) is winning for player to move. So from (1,2,4), if we move to (1,1,4), then opponent's turn, position (1,1,4) is winning for opponent, so we lose. So indeed both moves are losing, so (1,2,4) is losing.
Now (1,2,5): we had moves: to (1,1,5) which is losing for player to move (since 5-1=4 even). So that move gives opponent a losing position => winning move. So (1,2,5) has a winning move, thus (1,2,5) is winning.
Now (1,2,6): we had moves: a) to (1,1,6) which is winning for opponent (since 6-1=5 odd) => losing; b) to (1,2,5) which is winning for opponent (since we just determined (1,2,5) is winning) => losing. So (1,2,6) is losing.
Now (1,3,6): moves: to (1,2,6) which we found losing, so that move is winning; to (1,3,5) we haven't evaluated yet. But since there exists at least one winning move (to (1,2,6)), then (1,3,6) is winning.
Thus from initial (2,3,6), option 1 (subtract from 2) leads to (1,3,6) which is winning for opponent (since we found (1,3,6) winning). So that move is losing.
Option 2 (subtract from 3) leads to (1,1,3) which is losing for opponent (since we found (1,1,3) losing). So that move is winning. So there is a winning move. Wait we found (1,1,3) is losing for player to move. So if we move to (1,1,3), opponent gets a losing position, so that move is winning. So (2,3,6) should be winning for first player. But earlier sum-N=8 even predicted Second. So contradiction! Let's double-check carefully.
We must be careful about (1,1,3) outcome: earlier we determined single >1 number =3: number of moves left = 2 (decrements from 3 to 1: 3->2->1). Starting from (1,1,3), it's the opponent's turn. The opponent will make the moves. If there are 2 moves left, then opponent moves first, then first player moves second, then after opponent's second move? Let's simulate exactly: state (1,1,3). Player to move (call it P). P can only decrement the 3: becomes (1,1,2). Now state (1,1,2). Next player (Q) moves: decrement 2 -> (1,1,1). Now it's P's turn, but board is all 1s, so P loses. So from (1,1,3), the player to move loses because they make the first of an even number of moves, leaving opponent to make last move. So (1,1,3) is losing for player to move. So indeed it's a losing position (P-position). So moving to (1,1,3) gives opponent a losing position, so that move is winning for the mover.
Thus from (2,3,6), option 2 is winning. So (2,3,6) is a winning position for first player. So first player wins. That would mean First, but sum-N even would predict Second. So parity of sum-N fails for this example. Did we compute sum correctly? N=3, sum=2+3+6=11, sum-N=8 even. According to parity conjecture, Second should win, but our manual analysis suggests First can win by subtracting from 3 leading to (1,1,3) which is losing for opponent. However we must verify if that move is indeed allowed and if the resulting state is (1,1,3) after the division step. Let's recompute that move precisely.
Initial state: (2,3,6). Choose to subtract 1 from the 3. Then numbers become (2,2,6). Now compute gcd of (2,2,6). gcd(2,2,6) = 2. Then divide all numbers by 2: (1,1,3). Yes.
Thus resulting state is (1,1,3). As argued, (1,1,3) is losing for the player to move (the opponent). So first player wins. So the parity conjecture is false. But wait, example 3 from the problem gave Second when sum-N even? It matched, but here it doesn't. Did we miss something about the game rules? Perhaps our analysis of (1,1,3) as losing might be wrong because of the normalization step: In (1,1,3), all numbers are not necessarily coprime? Actually (1,1,3) gcd=1, okay. The moves: we can subtract from 3, then we get (1,1,2). Compute gcd of (1,1,2) = 1. Then no division. That's correct. Then subtract from 2 gives (1,1,1). So indeed two moves. So the player who moves from (1,1,3) loses. So it's a losing position.
Thus (2,3,6) is a winning position. But is (2,3,6) a valid initial state? It satisfies gcd=1? gcd(2,3,6)=1 yes. So it should be a valid input. According to our parity guess, it would output Second, but we find First. But the problem examples didn't include this case. Let's quickly test by writing a small program mental? We'll double-check using recursion perhaps with more thorough analysis, but it seems plausible.
But wait, maybe we missed that after subtracting from 3, we got (2,2,6) and then division by 2 gave (1,1,3). But what if there is also the possibility that after subtraction, we must divide by g and then maybe there are further reductions? No, it's just one division.
So it appears parity of sum - N is not sufficient.
We need to find the correct criterion.
Let's explore more properties.
Observations:
-
The move reduces the total sum significantly when gcd>1 after subtraction. But we can think of the state in terms of the multiset after dividing by common factors, which ensures gcd=1.
-
The game is impartial, so we can compute Grundy numbers? But N up to 1e5, numbers up to 1e9, so there must be a simpler characterization.
Maybe the outcome depends on the parity of the number of moves in some "canonical" game reduced to Nim heap of sizes something like "the number of odd numbers" or "the number of ones"? Or perhaps it's determined by the parity of the total number of prime factors? Or something like that.
We can attempt to analyze the game as a variant of Euclid's algorithm or like the "Turning Turtles" game where each move subtracts 1 then divides by gcd. This is reminiscent of a known problem: AtCoder "ABC.."? Possibly from AtCoder "AGC 027 C"? Not sure.
Maybe we can think in terms of binary representation? Because dividing by gcd often removes common powers of 2? Let's examine small states.
Define f(state) as winning/losing.
We've computed some states:
- Terminal: all ones => losing (P-position).
- Single >1 number k: losing if (k-1) even, winning if odd.
- Two numbers both >1 with gcd=1? We analyzed some: (2,3) winning, (2,5) winning, (3,5) losing. (3,4) winning. (2,9)? Let's test quickly: (2,9) sum-N = 9? Actually N=2, sum=11, sum-N=9 odd => maybe winning. But our parity guess already flawed.
But maybe there's a simpler rule: The outcome is determined by parity of total number of moves if both players play optimally? But the existence of choices that change the number of moves drastically due to division suggests that the game is not just a simple count.
Another direction: Consider the concept of "invariant: the sum of numbers modulo something"? Or perhaps the game is equivalent to playing Nim on the set of numbers after subtracting 1? Wait, after subtracting 1, dividing by gcd may reduce many numbers simultaneously. This is similar to the game of "Dawson's Kayles"? No.
We can attempt to transform the game to one on the multiset of prime factors, or maybe to the "gcd of all numbers" concept.
Idea: Let’s consider the state as a multiset of integers with gcd=1. The move: choose i with a_i>=2, set a_i := a_i - 1, then let g = gcd(all a_j), then divide all a_j by g.
Note that after division, the numbers become relatively prime. So the state can be thought of as a set of numbers with no common factor.
Observation: The operation of subtracting 1 and then dividing by gcd can be seen as an inversion of the operation of multiplying by something and adding 1? Perhaps there's a known result: This game is equivalent to Euclid's algorithm for computing GCD, and the outcome is determined by the parity of the total number of prime factors of the product of numbers? Something like that.
Let's try more small states to see pattern.
We'll manually compute Grundy for small numbers using programmatic reasoning in mind, but we can do systematic analysis for small values.
Since N can be up to many, but numbers can be large, maybe the result is simply: First if the total sum of (a_i - 1) is odd? But we saw counterexample (2,3,6): sum - N = 8 even but we found winning for first. So not that.
Maybe it's First if the total sum of (a_i) is odd? For (2,3,6): sum=11 odd => would predict First, which matches. For examples: ex1 sum=16 even? 3+6+7=16 even? Actually 3+6+7=16 even, but output First. So sum parity fails (First even though sum even). ex2 sum=15 odd => First. ex3 sum=39 odd? 7+8+8+8+8=39 odd => Second. So sum parity not consistent: ex1 sum even but First; ex3 sum odd but Second. So no.
Maybe it's about parity of number of even numbers? Let's compute: ex1: numbers: 3 odd,6 even,7 odd -> count even =1 (odd count)? 1 is odd, output First. ex2: 1 odd,2 even,4 even,8 even -> even count =3 odd -> First. ex3: 7 odd,8 even,8 even,8 even,8 even -> even count =4 even -> Second. That suggests: First if number of even numbers is odd? For (2,3,6): evens: 2 and 6 => 2 even numbers (even) => would predict Second, but we got First. So fails.
Maybe it's about parity of total number of ones? Not.
Let's step back. Maybe my manual analysis of (2,3,6) might have missed an option for the opponent in (1,1,3) that could change the outcome? But (1,1,3) seems straightforward losing for player to move. Could there be a move from (1,1,3) that leads to a state with division that changes the count? Let's enumerate all moves from (1,1,3). Only move: subtract 1 from 3 -> (1,1,2). Then from (1,1,2): subtract 1 from 2 -> (1,1,1). So yes two moves. So it's a losing position.
Thus (2,3,6) is indeed winning. So the parity of something else.
Maybe the outcome is determined by the parity of the total number of "moves" in a "canonical" sequence that reduces the numbers to all ones by always subtracting from the largest? But players have choices.
Perhaps we can think of the game in terms of the mex of options, but we need a simple rule.
Let's compute more positions systematically for small numbers with gcd=1. Since N up to 1e5, but numbers could be small. Possibly the result is: First wins except when all numbers are odd? No: ex3 has four evens, one odd, not all odd. ex2 has evens and odd. ex1 has mix.
Maybe it's about the parity of the sum of (a_i - 1) after dividing by powers of 2? Let's examine in binary.
We notice that the division step divides by the gcd. The gcd is often 1, but sometimes becomes >1 after subtracting 1. Subtracting 1 can create common factors. For example, from (2,3,6), subtract from 3 gave (2,2,6) with gcd 2. That division reduced the numbers drastically, essentially dividing all numbers by 2. That is a powerful move.
Maybe we can view the game in terms of the "odd part" of numbers? Because subtracting 1 toggles parity, and division by 2 may happen if all numbers become even. So maybe the game is about the parity of the count of numbers that are even? But we saw counterexample.
Let's analyze (2,3,6) more deeply, see if maybe there is an alternative move for first player that leads to win, but we already found one. So it's winning. So any rule that predicts losing for (2,3,6) is wrong.
But maybe we miscomputed (2,3,6) because we assumed optimal play for opponent? In (2,3,6) we found a winning move: subtract from 3. That leads to (1,1,3). Then opponent loses. So it's a winning position. So rule: first wins if there exists a move that leads to a losing position. So we need to characterize positions.
Maybe the Sprague-Grundy value is determined by the XOR of something like (a_i - 1)? But with division step, it's not straightforward.
Let's attempt to compute Grundy for small states with a programmatic approach by hand, maybe for two numbers (a,b) with gcd=1 and a,b >=1. But note that after normalization, the state always has gcd=1. So for two numbers with gcd=1, we can analyze the game. Let's try to compute outcome for all small pairs (a,b) with a<=b, gcd=1, and a,b positive, not both 1 (since terminal is losing). We'll compute P/N (P-position = losing for player to move, N-position = winning). Let's list:
Terminal: (1,1) is losing.
Consider (1,k) with k>=2. This is like single >1 number. Moves: only subtract from k, resulting (1,k-1) (since gcd=1). So it's a simple countdown. So (1,k) is P if (k-1) even, N if odd. So:
(1,2): k-1=1 odd => N
(1,3): 2 even => P
(1,4): 3 odd => N
(1,5): 4 even => P
(1,6): 5 odd => N
So pattern: (1,even) is N? Actually k=2 even => N; k=4 even => N; k=6 even => N. (1,odd) gives P for odd>=3? (1,3) P, (1,5) P. So (1,odd) P, (1,even) N.
Now for (2,3): we computed N. (2,4) not allowed because gcd=2. (2,5): we computed N? Actually earlier we said (2,5) is winning? Let's recalc (2,5) directly: State (2,5). Moves:
- Subtract from 2: -> (1,5) (gcd=1) => becomes (1,5) which is P (since 5 odd? Actually (1,5) is P because 5-1=4 even, so losing for player to move). So that move leads to P, which is winning for mover? Wait: If we move to a P position, that's a winning move. So from (2,5), moving from 2 gives opponent a losing (P) position, so (2,5) is N.
- Subtract from 5: -> (2,4). gcd(2,4)=2, divide -> (1,2) which is N (since (1,2) is N). So that leads to N, not good. So since there is at least one move to P, (2,5) is N.
Thus (2,5) N.
(2,7): similar? Subtract from 2 -> (1,7): 7-1=6 even => (1,7) P => that move is winning, so (2,7) N. So likely (2, odd>=3) is N? Because (1,odd) is P, so move from 2 to (1,odd) yields P. So (2,odd) is N. What about (2,9) N.
(3,4): we computed N (since move from 4 to (3,3)->(1,1) terminal P, so winning). So (3,4) N.
(3,5): we computed P (both moves lead to N). Let's verify:
State (3,5) with gcd=1. Moves:
- Subtract from 3 -> (2,5) which we found N.
- Subtract from 5 -> (3,4) which is N.
Thus both to N => (3,5) P.
(3,7): let's compute. Moves:
- Subtract from 3 -> (2,7). (2,7) we suspect N? (2,7) N. So leads to N.
- Subtract from 7 -> (3,6). gcd(3,6)=3 -> divide -> (1,2) which is N.
So both to N => (3,7) P? But need check (2,7) is indeed N. Yes from earlier (2,odd) N. So (3,7) both moves to N => P.
(3,8): moves:
- Subtract from 3 -> (2,8). gcd(2,8)=2 -> divide -> (1,4) which is N (since (1,4) N). So leads to N.
- Subtract from 8 -> (3,7). We suspect (3,7) is P. So that move leads to P, which would be a winning move. So (3,8) would be N because exists move to P. So (3,8) N.
Thus pattern: (3, odd) seems P? (3,5) P, (3,7) P. (3, even) maybe N? (3,4) N, (3,8) N.
(4,5): gcd=1. Compute.
Moves:
- Subtract from 4 -> (3,5). (3,5) is P. So that move to P => (4,5) N? But need check other move maybe also leads to P? But at least one to P gives N.
- Subtract from 5 -> (4,4). gcd(4,4)=4 -> divide -> (1,1) terminal P. So also to P. So both to P? Actually terminal is losing for next player, i.e., P. So moving to terminal is winning move because opponent cannot move. So both moves lead to P? Wait careful: Terminal is P (losing for player to move). So if we move to terminal, opponent gets a P position, so that's a winning move. So both moves lead to P, so (4,5) N. So it's N.
(4,7): moves:
- from 4 -> (3,7). (3,7) P -> N? Actually (3,7) is P, so move to P is winning.
- from 7 -> (4,6). gcd(4,6)=2 -> divide -> (2,3). (2,3) is N. So there is a move to P? Actually (2,3) is N, not P. But the move from 4 to (3,7) gives P, so (4,7) is N.
So (4,7) N.
(4,9): from 4 -> (3,9). gcd(3,9)=3 -> divide -> (1,3). (1,3) P. So N.
(5,6): gcd=1? gcd(5,6)=1. Compute:
- Subtract from 5 -> (4,6). gcd(4,6)=2 -> (2,3). (2,3) N.
- Subtract from 6 -> (5,5). gcd(5,5)=5 -> (1,1) terminal P.
So there is move to P, so (5,6) N.
(5,7):
- from 5 -> (4,7). (4,7) N.
- from 7 -> (5,6). (5,6) N.
So both to N => (5,7) P? Check if any other? That's it. So (5,7) P.
(5,8):
- from 5 -> (4,8). gcd(4,8)=4 -> (1,2) N.
- from 8 -> (5,7). (5,7) P (if we determined P). So move to P => (5,8) N.
(5,9):
- from 5 -> (4,9). (4,9) N? Actually we didn't compute (4,9) but we earlier considered (4,9) from 4 gives (1,3) P, so (4,9) is N (since it has move to P). So (4,9) N.
- from 9 -> (5,8). (5,8) N (just found N). So both to N => (5,9) P? Need verify (5,8) is N yes. So (5,9) P.
(6,7): gcd=1. Compute:
- from 6 -> (5,7). (5,7) P => so move to P => N? Then (6,7) N.
- from 7 -> (6,6). gcd(6,6)=6 -> (1,1) P => also to P. So (6,7) N.
(6,11):
- from 6 -> (5,11). Need to compute (5,11). Probably P? Let's see pattern: (a,b) with a,b both odd? For (5,11) both odd; maybe P? Let's compute later.
But from pattern, it seems that (odd, odd) might be P when both >1? Let's check: (3,5) both odd -> P; (3,7) both odd -> P; (5,7) both odd -> P; (5,9) one odd? both odd -> P; (1,odd) is P but that's a special case with a 1. (2, odd) -> N (since 2 even). (4,5) one even one odd -> N; (4,7) even-odd -> N; (4,9) even-odd -> N; (6,7) even-odd -> N; (3,4) odd-even -> N; (3,8) odd-even -> N; (5,6) odd-even -> N; (5,8) odd-even -> N; (6,11) even-odd -> likely N. So pattern: When both numbers are odd (and >1), the position appears to be P (losing). When at least one is even, it's N (winning). Check (2,3) had one even -> N, matches. (2,5) -> N. (2,7) -> N. (1,k) is special: (1,even) -> N, (1,odd) -> P. That also fits the pattern if we consider 1 as odd? 1 is odd. So (1,even) => one even => N; (1,odd) => both odd => P. So consistent: The losing positions (P) are exactly those where all numbers are odd (including 1 as odd). And winning positions (N) are those where at least one number is even.
Now test (2,3,6): numbers: 2 even, 3 odd, 6 even. At least one even, so according to this hypothesis, it should be N (First wins). We found First wins, consistent! Example1: 3,6,7: contains 6 even => N (First). Example2: 1,2,4,8: contains 2,4,8 even => N (First). Example3: 7,8,8,8,8: contains 8 even => N? But output is Second. Wait, example3 has evens (8's) so according to hypothesis it would be First, but actual output is Second. So contradicts. Let's re-check example3: Input: 5 numbers: 7,8,8,8,8. There are four 8's (even). So at least one even, would predict First, but output says Second. So hypothesis fails.
Maybe the condition is that all numbers are odd? Ex3 not all odd, but output Second, meaning that position is losing? Actually Second wins means the first player loses, i.e., the initial position is a P-position. In example3, there are evens, yet it's P? So our hypothesis that P positions are exactly all odds is false.
But maybe we need to consider the effect of having more than two numbers? In two-number case, pattern seemed to hold: all odds -> P, else N. But for multiple numbers, it may be more complex. Example3 has multiple evens, but still P.
Let's compute (7,8,8,8,8) manually? That's more complex. But we can try to analyze if it's P or N by reasoning.
State: (7,8,8,8,8). gcd is 1 (since 7 odd). According to hypothesis, it might be P. But we need to verify.
We need to evaluate the position. Could it be that the game reduces to Nim on the parity of count of numbers that are >1? Or something like the mex of Grundy values of each number? But division by gcd couples numbers.
Maybe we need to consider the total number of moves modulo something, but example3 seems to be losing despite having evens.
Let's attempt to compute the outcome for (7,8,8,8,8) by exploring options.
We have one 7 (odd) and four 8's (even). Let's denote the multiset.
Because all numbers are equal except one 7, maybe we can find a symmetry.
First, note that the numbers are large, but the game will reduce eventually.
We need to see if there is a move from this state that leads to a losing position for the opponent. If none, then it's losing.
Given the pattern from two-number case, maybe the outcome is determined by the parity of the sum of (a_i - 1) divided by something? Actually example3 sum - N = 34 even but we found other even sum-N cases that were winning. So not.
Maybe the key is to consider the numbers modulo something like the total number of odd numbers? For (2,3,6) we had two evens (2,6) and one odd (3) -> number of evens = 2 (even). Example1: evens count=1 (odd). Example2: evens count=3 (odd). Example3: evens count=4 (even). So maybe First wins if the number of even numbers is odd, and loses if even. Check (2,3,6): evens=2 even -> would predict Second, but we found First. So no.
Maybe it's about the parity of the number of numbers that are congruent to something mod something else.
Let's think about the effect of division by gcd. When we subtract 1 from a number, it may cause all numbers to become even if they all are odd? For instance, if all numbers are odd, subtracting 1 from any odd makes it even, and then gcd of all numbers might become 1? Actually if all numbers are odd, and we subtract 1 from one odd, that number becomes even. Then the set contains both odds and evens. The gcd of a set containing both odd and even numbers could be 1 if there's an odd number that is not multiple of something. But it's possible that gcd becomes >1 if all numbers become even? But there are still odd numbers present, so gcd cannot be even? Actually if there is at least one odd, the gcd cannot be even because an odd number is not divisible by 2. So gcd will be odd, possibly 1. So division by gcd >1 may still happen if all numbers share an odd factor. For example, all numbers odd could share factor 3? But given initial gcd=1, it's possible that after subtract, they could gain a common odd factor? Possibly.
But maybe the game is equivalent to the impartial game where each move reduces the sum of numbers, and the Grundy value is the XOR of Grundy values of each number treated independently? Could the division step couple them? However, dividing by common gcd effectively scales all numbers down, which could be seen as a simultaneous move on all numbers, making it not separable.
Maybe we can consider representing the state by the multiset of numbers after factoring out any common factor? But the state already has gcd=1. However, note that after a move, we divide by the new gcd, which may be >1. That division could be thought of as "normalizing" the state. This is reminiscent of the game "Euclid's game" or "Chocolate Bar" but not exactly.
Another angle: The process of subtracting 1 and then dividing by gcd is similar to the classic game "Turning Turtles" but not.
Maybe we can think in terms of the binary representation of numbers: subtracting 1 flips the lowest bit. Dividing by gcd (which could be a power of 2 if all numbers become even) removes common factors of 2. So the game might be related to the number of trailing zeros in each number (the 2-adic valuation). Indeed, if all numbers are even, gcd is at least 2, and dividing by that power of 2 reduces the numbers and possibly changes parity.
Let v2(x) denote the exponent of 2 in x (i.e., the number of trailing zeros). The gcd of numbers will have v2(g) = min_i v2(a_i) if all numbers are even; otherwise if any odd, min_i v2(a_i) = 0 because odd has v2=0, so gcd is odd. So after subtraction, if the resulting set has all numbers even, then the min v2 >0, so gcd is divisible by 2. Then dividing by gcd reduces the v2's.
So maybe the outcome depends on the parity of total number of moves, but the division step can skip many moves.
Let's attempt to compute Grundy for small states with N=3 to see pattern beyond two numbers.
We already computed (2,3,6) is N. Let's compute some other three-number states to see if they follow a simple rule.
Consider all numbers odd: e.g., (3,5,7) all odd. Is this P or N? Let's test small: (1,1,1) terminal P. (1,1,3) we computed P. (1,3,5) maybe? (1,3,5) all odd? 1 odd, 3 odd, 5 odd. According to two-number pattern, if all odd, maybe P. But need check.
Compute (1,3,5). Moves: we can subtract from 3 or 5 (since 1 can't).
- Subtract from 3: (1,2,5). gcd(1,2,5)=1 -> (1,2,5). We earlier computed (1,2,5) is N (since we found (1,2,5) winning). So this leads to N.
- Subtract from 5: (1,3,4). gcd(1,3,4)=1 -> (1,3,4). Need evaluate (1,3,4). Is (1,3,4) P or N?
(1,3,4): moves: from 3 -> (1,2,4) we computed (1,2,4) as losing (P) earlier? Actually we computed (1,2,4) as losing? Wait we computed (1,2,4) and found it losing (P) because both moves lead to winning for opponent. Yes we determined (1,2,4) is losing. So (1,2,4) is P. From (1,3,4), subtract from 3 leads to (1,2,4) which is P, so that move would be winning. Therefore (1,3,4) is N (since exists move to P). So (1,3,4) is N.
Thus from (1,3,5), subtract from 5 gives (1,3,4) which is N. So both moves from (1,3,5) lead to N? Actually subtract from 3 gave (1,2,5) which is N. So both lead to N, so (1,3,5) would be P? But we need to be sure about (1,2,5) we earlier found it winning (N). So yes both to N, so (1,3,5) is P. So all odd (1,3,5) appears to be P.
Now consider (1,3,7) maybe similar? Likely P.
Now consider (2,3,5): contains evens (2). Predict maybe N? Let's test.
State (2,3,5). Moves:
- Subtract from 2: (1,3,5) -> which we just found P. So that move leads to P, so (2,3,5) would be N. So yes N.
Now consider (3,5,7) all odd. Let's test if it's P.
We need to compute (3,5,7). Moves: many options.
But maybe we can find a counterexample where all odd yields N? Let's test (1,1,3) all odd but we found P. (1,3,5) P. (3,5,7) could be P.
Now example3 (7,8,8,8,8) contains evens but is P. So maybe the condition is not simply presence of even numbers; something else like the sum of something modulo 2? Let's compute parity of number of even numbers? In (7,8,8,8,8) evens count=4 even. In (2,3,6) evens count=2 even, but that was N. So evens parity alone not decisive.
Maybe it's about the total number of numbers with odd v2? Actually all evens have v2>=1, odd have v2=0. The gcd after moves may involve powers of 2.
Another idea: The game might be equivalent to a Nim heap where the size is the sum of (a_i - 1) after repeatedly dividing by common factors? Not clear.
Let's try to analyze the game more systematically.
Observation: The move consists of two steps: subtract 1 from a chosen number, then divide all numbers by their gcd. The division step can be seen as scaling down the entire configuration. If we think of the state as a set of numbers, we can consider the "total" as something like the product? Not.
Maybe we can think in terms of the "multiset of numbers modulo something". Another approach: Consider the following transformation: Define the state as the multiset of numbers after factoring out the gcd? But the state already has gcd=1. The division step ensures that after move, gcd=1.
Maybe we can model the game as a take-away game on the set of numbers where the move reduces the sum drastically, but the outcome depends on the parity of the total number of "reduction steps" in a canonical play where players always subtract 1 from the largest number? Not sure.
Given constraints N up to 1e5 and A_i up to 1e9, there must be a simple condition, perhaps based on the parity of the sum of floor(log2(a_i)) or something.
Let's try to compute Grundy for small tuples programmatically in our head for more patterns. But we need a more efficient way.
Maybe the game is equivalent to the game of Nim on the binary representations where each move corresponds to toggling the least significant bit and then removing common trailing zeros? That suggests a connection to the theory of turning tokens.
Consider the following: For any set of numbers, define the multiset of values after repeatedly removing common factors? But after each move, we divide by the gcd, which could be any common factor, not just 2.
But perhaps we can consider the prime factorization and note that subtracting 1 from a number tends to remove common factors with others? Not sure.
Let's try to find invariants. Consider the product of all numbers? After subtracting 1 from a_i, product becomes (a_i - 1) * (product of others). Then after dividing by g^N, product becomes (product of others)*(a_i - 1) / g^N. The reduction is not monotonic in an obvious way.