We need to analyze this impartial combinatorial game: multiple piles, each move: choose a pile with x coins, replace it with y where 0 ≤ y < x and x^{1/4} ≤ y ≤ x^{1/2}. The numbers are huge, up to 7.78e11? Actually ai up to 777777777777, which is about 7.78e11. The bounds involve x^{1/4} and x^{1/2}. This is an interesting move constraint. The game is impartial, so Grundy numbers (nimbers) for each pile can be computed, and overall xor determines winner (Furlo first, so non-zero xor => Furlo wins, else Rublo). But since constraints: n up to 77777, but each pile size up to 7.78e11, we cannot compute Grundy for each pile individually by DP over all possible states. Need to find pattern or closed form Grundy values.
We define f(x) = Grundy number for a pile of size x. f(0) is terminal (no move) presumably not used; but we only consider x>=1. Moves from x go to any y satisfying 0 ≤ y < x and floor? Actually condition: x^{1/4} ≤ y ≤ x^{1/2}. But careful: y must be integer. And y can be 0? If 0 is allowed? Condition: 0 ≤ y < x; x^{1/4} ≤ y ≤ x^{1/2}. For y=0, we require 0 >= x^{1/4}, which is false unless x=0. So y=0 is not allowed except maybe if x^{1/4} <= 0, which only if x=0. So y=0 is not a legal move because x^{1/4} >0 for x>=1. So the moves are to integers y in the interval [ceil(x^{1/4}), floor(x^{1/2})] but also < x. Since x^{1/2} < x for x>1, for x=1, x^{1/4}=1, x^{1/2}=1, so y could be 1? But y < x, so from x=1, there is no y satisfying both y<1 and 1≤y≤1. So no moves from 1. So Grundy(1)=0. Good.
Thus for each x, the set of reachable Grundy values is { f(y) : y integer, ceil(x^{1/4}) ≤ y ≤ floor(x^{1/2]) }.
We need to compute Grundy numbers for x up to ~7.78e11. Observations: x^{1/4} and x^{1/2} are about fourth root and square root. The interval [x^{1/4}, x^{1/2}] is quite wide for large x. For example, x=10^12, sqrt=10^6, fourth root=10^3. So interval length ~10^6 - 10^3 ~ 1e6. That's large but maybe still manageable? But n up to 77777, and each pile we might need to compute Grundy by iterating over all possible y in that interval? That could be huge for large x (sqrt up to ~10^6). Not too large? 10^6 is okay per pile? But if we have 77777 piles, that would be too much. But maybe the Grundy numbers are small and periodic? Might be we can compute f(x) analytically.
Let's explore small x manually to see pattern.
Define f(1)=0 (no moves).
x=2: sqrt(2)≈1.414, floor=1; fourth root 2^{0.25}≈1.189, ceil=2? Actually 2^{0.25}=~1.189, ceil=2? But 2 > sqrt? Wait: interval is [x^{1/4}, x^{1/2}], so for x=2, lower bound ~1.189, upper bound ~1.414. The integer y must satisfy y≥1.189 and y≤1.414, so only possible y= ? y=1? 1 is ≤1.414, but is 1≥1.189? No, 1<1.189. So no integer y. So f(2)=0? Check: y must be integer. For x=2, possible y? y=1 is less than 2, but does it satisfy 2^{1/4} ≤ 1? That's false. y=0? no. So no moves. So f(2)=0.
x=3: sqrt≈1.732 floor=1; fourth root≈3^{0.25}=3^{0.5^{0.5}}? Actually 3^{0.25}=sqrt(sqrt(3)) ≈ sqrt(1.732)=1.316, ceil=2? But 2 > sqrt, so no integer y? Let's compute: need integer y such that 1.316 ≤ y ≤ 1.732. Only y=2? 2>1.732, no. y=1? 1<1.316. So none. So f(3)=0.
x=4: sqrt=2, floor=2; fourth root=4^{0.25}=sqrt(2)≈1.414, ceil=2? Actually ceil(1.414)=2. So interval: [2,2], y=2 is allowed? Check y < x? 2 < 4, yes. So moves to y=2. So from 4 we can go to 2. f(2)=0, so reachable Grundy set {0}. So mex{0}=1, so f(4)=1.
x=5: sqrt≈2.236 floor=2; fourth root=5^{0.25}= sqrt(sqrt(5)) ≈ sqrt(2.236)=1.495, ceil=2. So interval: [2,2], y=2 allowed? 2<5, yes. So moves to 2 (f(2)=0). So f(5)=mex{0}=1.
x=6: sqrt≈2.449 floor=2; fourth root≈ sqrt(sqrt(6)) ≈ sqrt(2.449)=1.565, ceil=2. So y=2. f(2)=0 -> f(6)=1.
x=7: sqrt≈2.645 floor=2; fourth root≈ sqrt(sqrt(7)) ≈ sqrt(2.645)=1.626, ceil=2. So y=2 -> f(7)=1.
x=8: sqrt≈2.828 floor=2; fourth root≈ sqrt(sqrt(8)) ≈ sqrt(2.828)=1.681, ceil=2. So y=2 -> f(8)=1.
x=9: sqrt=3 floor=3; fourth root=9^{0.25}= sqrt(3)=1.732, ceil=2. So interval: from lower=2 to upper=3. So possible y=2 or 3? Check: 2 ≥ 1.732, yes; 3 ≤3, yes. So moves to y=2 (f(2)=0) and y=3 (f(3)=0). So reachable {0}. So f(9)=1.
x=10: sqrt≈3.162 floor=3; fourth root≈ sqrt(sqrt(10))≈ sqrt(3.162)=1.778, ceil=2. So interval: [2,3]. y=2,3. f(2)=0, f(3)=0. So f(10)=1.
x=11: sqrt≈3.316 floor=3; fourth root≈ sqrt(3.316)=1.821, ceil=2. -> y=2,3 -> f=1.
x=12: sqrt≈3.464 floor=3; fourth root≈ sqrt(3.464)=1.861, ceil=2. -> y=2,3 -> f=1.
x=13: sqrt≈3.606 floor=3; fourth root≈ sqrt(3.606)=1.899, ceil=2. -> y=2,3 -> f=1.
x=14: sqrt≈3.741 floor=3; fourth root≈ sqrt(3.741)=1.934, ceil=2. -> y=2,3 -> f=1.
x=15: sqrt≈3.873 floor=3; fourth root≈ sqrt(3.873)=1.968, ceil=2. -> y=2,3 -> f=1.
x=16: sqrt=4 floor=4; fourth root=16^{0.25}=2, ceil=2. So interval: [2,4]. Allowed y: 2,3,4. f(2)=0, f(3)=0, f(4)=1. So reachable Grundy set {0,1}. mex of {0,1}=2, so f(16)=2.
Thus f(4)=1, f(5-15)=1, f(16)=2.
Let's continue to see pattern.
x=17: sqrt≈4.123 floor=4; fourth root≈ 17^{0.25}=sqrt(sqrt(17))≈ sqrt(4.123)=2.030, ceil=3? Wait careful: 4.123 is sqrt, fourth root is sqrt(sqrt) so compute: sqrt(17)≈4.1231, then fourth root = sqrt(4.1231)≈2.0305, ceil=3? Actually ceil(2.0305)=3? No, ceil of 2.0305 is 3 only if it's >2? Ceil gives smallest integer >= number: 2.0305 ceil = 3? Actually 2.0305 is greater than 2, so the smallest integer >=2.0305 is 3? Wait: integers: 2 is less than 2.0305, so 2 is not >=, so ceil is 3? Let's check: ceil(2.1)=3, ceil(2.0001)=3? Actually ceil(2.0001)=3? In mathematics, ceil(2.0001)=3? I'm mistaken: ceil(2.0001) = 3? No, ceil(2.0001) is the smallest integer greater than or equal to 2.0001, which is 3? Wait 2.0001 is greater than 2, so the next integer is 3, but 2 is less than 2.0001, so it does not satisfy >=, so indeed the smallest integer >=2.0001 is 3 because 2 is not >=2.0001. So yes, ceil(2.0001)=3. So if lower bound is >2, then the smallest integer that is >= is 3. So for 17, fourth root ~2.0305, ceil=3. Upper bound floor(sqrt(17))=4. So interval: y from 3 to 4 inclusive. So possible y=3,4. f(3)=0, f(4)=1 => reachable {0,1}. mex {0,1}=2 => f(17)=2.
x=18: sqrt≈4.243, floor=4; fourth root≈ sqrt(4.243)=2.060, ceil=3. So y=3,4 -> f(3)=0, f(4)=1 -> f(18)=2.
x=19: sqrt≈4.359, floor=4; fourth root≈ sqrt(4.359)=2.088, ceil=3 -> y=3,4 -> f=2.
x=20: sqrt≈4.472, floor=4; fourth root≈ sqrt(4.472)=2.115, ceil=3 -> y=3,4 -> f=2.
... up to x=25? Let's compute x=24: sqrt≈4.899, floor=4; fourth root≈ sqrt(4.899)=2.213, ceil=3 -> y=3,4 -> f=2.
x=25: sqrt=5 floor=5; fourth root=25^{0.25}= sqrt(5)=2.236, ceil=3. So interval [3,5]. y=3,4,5. f(3)=0, f(4)=1, f(5)=1? Actually f(5)=1 from earlier. So reachable {0,1}. mex=2 => f(25)=2.
x=26: sqrt≈5.099, floor=5; fourth root≈ sqrt(5.099)=2.258, ceil=3 -> y=3,4,5 -> f=2.
Continue until maybe some point where f changes.
Let's compute x=36: sqrt=6, floor=6; fourth root=36^{0.25}= sqrt(6)=2.449, ceil=3? Actually 2.449 ceil=3. So interval [3,6]. y=3,4,5,6. f(3)=0, f(4)=1, f(5)=1, f(6)=1. So reachable {0,1} -> f=2.
x=49: sqrt=7, floor=7; fourth root= sqrt(7)=2.646, ceil=3 -> interval [3,7]; f(3)=0, f(4)=1, f(5)=1, f(6)=1, f(7)=1 => {0,1} -> f=2.
x=64: sqrt=8, floor=8; fourth root= 64^{0.25}= (64^{1/4}) = (2^6)^{1/4}=2^{1.5}=2*sqrt(2)=2.828? Actually 64^(1/4)= sqrt(sqrt(64))= sqrt(8)=2.828, ceil=3. So interval [3,8]. f(3)=0, f(4)=1, f(5)=1, f(6)=1, f(7)=1, f(8)=1 => {0,1} -> f=2? But wait f(16)=2, and we might get reachable includes 2 eventually. At x=64, does the interval include 16? 16 is >8? Upper bound is 8, so no. So f remains 2. But maybe at some larger x, interval includes numbers with Grundy 2, thus leading to mex possibly 3.
We need to see when f becomes 3. For f(x) to be 3, the set of reachable Grundy values must contain 0,1,2 but not 3. So we need that from x, we can move to piles with Grundy 0, 1, and 2. Since f(2)=0, f(4)=1, f(16)=2, etc. But we also have many numbers with Grundy 1 (like 4-15, and maybe others). And numbers with Grundy 2 start at 16? Actually f(16)=2, f(17-? ) =2 up to some range. Let's compute further to see when f becomes 3.
We should compute f for larger numbers to detect pattern. Since the move interval is [x^{1/4}, x^{1/2}], which for larger x includes many numbers. The Grundy numbers likely increase slowly, and could be related to the number of times you need to take fourth root? Possibly f(x) is essentially the number of times you can apply the transformation until you get to 1? But the move reduces x to something between x^{1/4} and x^{1/2}. That's a significant reduction. So the game length (number of moves) is logarithmic in log log x? Because each move reduces the size roughly to at most sqrt(x). Starting from x, after one move, size ≤ sqrt(x). After second move, size ≤ sqrt(sqrt(x)) = x^{1/4}. But wait careful: from a pile of size y, the move reduces to between y^{1/4} and y^{1/2}. So if we apply repeatedly, the size decreases quickly. The maximum number of moves from a pile might be O(log log x). Indeed, if we always take the smallest possible (y = ceil(y^{1/4})? Actually the worst-case for number of moves is when we reduce as little as possible, i.e., to the maximum allowed: y = floor(x^{1/2}). Then from that new size, again reduce to its sqrt, etc. So the size sequence: x0 = x, x1 = floor(x0^{1/2}), x2 = floor(x1^{1/2})? But note the lower bound is x^{1/4}, not necessarily sqrt; but the move can choose any y in that interval. To maximize number of moves, we would choose the largest allowed y (to keep pile as large as possible). So effectively we can keep taking square roots. After k moves, size ~ x^{1/2^k}. The game ends when we reach 1 (no moves). So number of moves roughly until 2^{2^k} exceeds x? Actually when x^{1/2^k} < 2? For x up to 1e12, 2^k such that 2^{2^k} > x. 2^2=4, 2^4=16, 2^8=256, 2^16=65536, 2^32=4.29e9, 2^64 huge. So with x1e12, after 1 move: sqrt1e6, after 2: sqrt(1e6)=1000, after 3: sqrt(1000)~31, after 4: sqrt(31)~5, after 5: sqrt(5)~2 (but from 2 no move? Actually from 2 no move, but from 5 you can move to 2? 5 interval [2,2] -> 2. So after 5 moves you reach 2? Actually 2 is terminal? Wait from 2 there is no move, so if you reach 2 the game ends. But from 5 you can move to 2. So the number of moves from x is around floor(log2(log2(x)))? Actually it's about the number of times you can take square root until you get below 2. But note moves can also skip; Grundy might equal the number of moves in optimal play? Possibly the Grundy number equals the number of times you can apply the operation? But in impartial games where each move reduces to a smaller pile and moves are to any number in a certain range, the Grundy often equals the Grundy of the "distance" in some sense. But our computed values: f(1)=0 (0 moves), f(2)=0 (0 moves? Actually from 2, no moves, so zero), f(4)=1 (can move to 2, so one move), f(16)=2 (can move to numbers that have Grundy 0 and 1, so two moves possible? Actually from 16, you can move to 2 (f=0), 3 (0), 4 (1), etc. So you can go to positions with Grundy 0 and 1. So the Grundy being 2 indicates that the maximum number of moves if both play optimally? Not exactly; in impartial games where each move reduces a parameter and the options are exactly to all smaller values (like subtraction game), the Grundy equals the mex of Grundy of all smaller numbers. But here options are limited to a range.
Let's try to compute more manually up to maybe 256 to see pattern.
We need f(x) for x from 1 to maybe 1000. But we suspect f(x) depends on intervals defined by powers of 2 or maybe powers of something. Let's compute systematically.
Define for each x, let L = ceil(x^{1/4}), R = floor(x^{1/2}). The moves are to any y in [L, R] ∩ ℕ.
We need f(y) for y in that range. Since n can be large but x up to ~7.78e11, the number of distinct Grundy values might be small (like up to 6 or so). Because each step roughly squares the "level". For example, starting from numbers with Grundy 0: those are {1,2,3}. Actually 1,2,3 have Grundy 0. Then numbers with Grundy 1: from our computation, seems numbers from 4 up to 15 have Grundy 1? Let's confirm: we computed f(4)=1, f(5-15)=1, f(16)=2, f(17-? ) =2. What about 15? we computed f(15)=1. So numbers 4-15: Grundy 1. Check 15: earlier we said f(15)=1. Yes. So 1-3:0, 4-15:1, 16-? 2. Let's compute f(31?) Actually we haven't computed f(31) yet. But we need to find where f becomes 3.
Compute f for numbers around 64, we had f(64)=2. What about 63? For x=63: sqrt≈7.937 floor=7; fourth root≈ sqrt(sqrt(63)): sqrt(63)≈7.937, then sqrt of that ≈2.819, ceil=3. So interval [3,7]. y=3,4,5,6,7. f(3)=0, f(4)=1, f(5)=1, f(6)=1, f(7)=1 -> reachable {0,1} => f=2. So indeed many numbers from 16 up to something have f=2.
Now, what is the first x where f(x)=3? It must be when the interval [L,R] includes at least one number with Grundy 2, and also includes numbers with Grundy 0 and 1, so that mex becomes 3. Currently numbers with Grundy 2 start at 16. So for f(x) to be 3, we need L <= 16 <= R, and also need 0 and 1 present. But 0 and 1 are present via smaller numbers like 2-3 for 0 and 4-15 for 1, which are also within [L,R] if L is small enough. However, note that the interval [L,R] for x large will include many small numbers. But the condition L <= some number, but also L could be > some numbers. So for f(x) to be 3, we need that the interval contains at least one number with Grundy 0, at least one with Grundy 1, and at least one with Grundy 2, but does not contain a number with Grundy 3. So if the interval starts above 3, then Grundy 0 may not be present. So L must be <=3 to get Grundy 0. For Grundy 1 we need numbers 4-15; L must be <=15 and R>=4. For Grundy 2 we need numbers >=16. So we need L <= 3 (to include 2 or 3) and R >= 16. Let's see when this occurs: L = ceil(x^{1/4}) <= 3 => x^{1/4} <= 3 => x <= 3^4 = 81. So for x <=81, x^{1/4} <=3, so L <=3. But also need R = floor(x^{1/2}) >= 16 => x^{1/2} >=16 => x >=256. So simultaneously L<=3 and R>=16 requires x <=81 and x >=256, impossible. So that condition can't be satisfied. That suggests maybe Grundy 0 can also be achieved via numbers >3 with Grundy 0? But we only have Grundy 0 for {1,2,3} so far. Could there be larger numbers with Grundy 0? Possibly yes, if the interval from those numbers contains all Grundy values 0,1,... maybe resulting in mex higher, but it's possible some numbers have Grundy 0 later. For impartial subtraction-like games, it's not uncommon that Grundy numbers are not monotonic but perhaps they are non-decreasing? Let's check: we saw f(1-3)=0, f(4-15)=1, f(16-?)=2. Might f become 0 again? Possibly but seems unlikely because as x increases, the move options become richer, so Grundy likely increases. However, it's possible that for some x, the set of reachable Grundy includes 0,1,2,... up to k, forcing mex = k+1, thus increasing. So Grundy numbers likely are non-decreasing? Let's test monotonicity: f(4)=1, f(5)=1, f(15)=1, f(16)=2, f(17)=2, so it increased at 16. So far non-decreasing. Might it stay 2 for a long time then jump to 3 at some threshold. Let's try to find the smallest x such that the interval [L,R] includes numbers with Grundy 0,1,2. We need a y0 with f=0, y1 with f=1, y2 with f=2 within [L,R]. As argued, 0 only at {1,2,3} currently. But maybe later, there could be numbers >3 with f=0? If Grundy becomes 0 again, that would be periodic. Let's test if any x >3 can have f=0. For f(x)=0, the set of reachable Grundy numbers must not include 0. Actually mex of empty set is 0, but if there are moves, the reachable set must not contain 0. Could that happen? Possibly if all moves go to positions with Grundy >0, and none have Grundy 0. But since we have many positions with Grundy 0 only at 1,2,3, if the interval does not include 1,2,3, then f(x) might not have 0 reachable. For x sufficiently large, L = ceil(x^{1/4}) could be >3, thus the interval might not include 1,2,3. So it's possible that for x where L>3, the reachable set might not contain any 0, so f(x) could be 0 if also it doesn't contain other numbers that force mex to be >0? Wait f(x)=0 means the mex of reachable Grundy set is 0, i.e., 0 is not in the set. That is possible if all reachable Grundy numbers are >0 or the set is empty. But if the reachable set is nonempty and does not contain 0, then mex could be 0? No, mex of a set S of non-negative integers is the smallest non-negative integer not in S. If 0 is not in S, then mex = 0 regardless of other numbers? Actually if 0 is not present, the smallest missing non-negative integer is 0, so yes mex = 0. So f(x) could be 0 even if there are moves, as long as none of the moves lead to a position with Grundy 0. So later numbers could have Grundy 0 again. That would break monotonicity. But is that plausible? Let's test a possible x. For x large, L could be say 10, R could be large. The reachable Grundy values are f(y) for y in [L,R]. If f(y) never equals 0 in that range, then f(x)=0. But do we have any y>3 with f(y)=0? Not from our computed ones up to 64. But maybe later, some y could have f(y)=0 if their intervals don't include any 0 positions. For that to happen, for such y, L_y > 3 so it excludes 1,2,3. And also its reachable set doesn't contain 0. That means the Grundy numbers of numbers in [L_y, R_y] are all ≥1. That could happen if all numbers in that interval have Grundy ≥1. Since we suspect Grundy numbers tend to increase, but they might not dip to 0. However, if eventually there is a gap where Grundy 0 is missing, then some number could have Grundy 0. But note that if there is any number with Grundy 0 in some range, then numbers that can move to it would have Grundy ≠0. But could a number with large x have all reachable Grundy values ≥1? Possibly if L is > the maximum index of Grundy 0 numbers. But since Grundy 0 numbers are only 1,2,3, if L >3, then the interval does not include 1,2,3. So the reachable numbers are all >=4. So if all numbers from 4 upwards have Grundy >=1, then f(x) would be at least 0 but might be 0 if mex of reachable set is 0? But if reachable set contains some numbers with Grundy 1, then 0 is missing, so mex could be 0. Wait: mex = smallest non-negative integer not in S. If S contains 1, then 0 is not in S, so mex = 0. So f(x)=0. That's possible! So if there is any number y in [L,R] with f(y)=1, and no y with f(y)=0, then f(x)=0. That would be a return to 0. Let's test if this can happen for some x. For x such that L>3 (so no 0), and there is at least one y with f(y)=1 in the interval, then S contains 1, so 0 is missing => mex=0 => f(x)=0. So indeed, numbers could have Grundy 0 again. But would that be consistent with our earlier computed values? For x from 4 to 15, we had Grundy 1. For x=16, we had Grundy 2, not 0. Let's see if for any x>15, f(x) could be 0. Let's test x=17: we computed f(17)=2, not 0. The reachable y were 3 and 4. f(3)=0, f(4)=1. So S contained 0 and 1 => mex=2. So 0 was present. For x=18-... up to some threshold, the interval likely includes y=3 or maybe y=2? Actually as x increases, L = ceil(x^{1/4}) will eventually exceed 3. At what x does L become >=4? L >=4 when x^{1/4} > 3 => x > 81. So for x > 81, L >=4 (since x integer, x>=82 gives x^{1/4} > 3.something? Let's compute: 3^4=81, 82^{1/4} ≈ 3.01, ceil would be 4). So for x >= 82, L >=4. So for x >=82, the interval [L,R] does NOT include 1,2,3. Therefore, from such x, you cannot move to any Grundy 0 position (assuming Grundy 0 only at 1,2,3). So if for some x>=82, the reachable Grundy set S does not contain 0, then mex could be 0 if S does not contain 0. But S will contain some Grundy values (since there are moves). If S contains 1 but not 0, mex=0. If S contains 0, mex>0. So f(x)=0 for x>=82 if all reachable y have Grundy at least 1, and at least one has Grundy 1 (so 0 missing, mex=0). But if S contains only numbers with Grundy >=2, then 0 and 1 missing, so mex could be 0? Actually if S contains only numbers with Grundy 2 and 3 etc, then the smallest non-negative integer not in S is 0, so mex=0 as well. So essentially if 0 is not in S, mex=0 regardless of other numbers. So f(x)=0 whenever 0 is not reachable. So for x>=82, if we can avoid reaching a position with Grundy 0, then f(x)=0. But are there any positions with Grundy 0 besides 1,2,3? Possibly later numbers could also be Grundy 0 if they satisfy similar condition. So we need to compute Grundy numbers for all x up to max a_i (7.78e11). But maybe the Grundy numbers are periodic or determined by intervals based on powers.
Let's compute further for x from 82 upward to see if any get f=0. But before doing that, we need to compute Grundy numbers for numbers beyond 81. Since L >=4 for x>=82, the interval [L,R] does not include 1-3. So the reachable Grundy numbers are f(y) for y between L and R, where y >=4. So f(x) will depend on f of numbers >=4. This suggests that the Grundy numbers might become 0 again for some x if the set of f(y) in that range does not contain 0. But it's also possible that for all x>=82, f(x) > 0. Let's test a specific x=82:
Compute x=82. sqrt(82)≈9.055, floor=9. fourth root: sqrt(sqrt(82)): sqrt(82)≈9.055, then sqrt of that ≈3.009, ceil=4? Actually 3.009 ceil=4. So L=4, R=9. So possible y = 4,5,6,7,8,9.
We need f(4)=1, f(5)=1, f(6)=1, f(7)=1, f(8)=1, f(9)=1? Wait f(9) we computed earlier: from 9 moves to 2 and 3, both Grundy 0, so f(9)=1. Yes f(9)=1. So all these y have Grundy 1. So reachable set S = {1}. Since 0 is not in S, mex = 0? Wait mex({1}) = 0 because 0 is smallest non-negative integer not in the set. So f(82)=0. That would be interesting! But we must check if any y in [4,9] have Grundy 0? They all have Grundy 1, so yes S={1}. So mex=0 => f(82)=0. However, we must double-check f(9) because earlier we computed f(9)=1. Yes. So f(82) would be 0. But we previously thought f(x) might be non-decreasing, but this shows it can drop to 0. So our earlier assumption that Grundy only increases is wrong.
Thus we need to compute f systematically for numbers up to perhaps some large range, but given max a_i ~ 7.78e11, the number of distinct Grundy values might be limited, and the pattern might be periodic in terms of intervals defined by powers of something like squares and fourth powers.
Let's compute more values to detect pattern.
We have:
x: f
1:0
2:0
3:0
4:1
5:1
6:1
7:1
8:1
9:1
10:1
11:1
12:1
13:1
14:1
15:1
16:2
17:2
18:2
19:2
20:2
21:2
22:2
23:2
24:2
25:2
26:2
27:2? Let's compute 27: sqrt≈5.196 floor=5; fourth root≈ sqrt(5.196)=2.279, ceil=3 -> y=3,4,5. f(3)=0, f(4)=1, f(5)=1 => S={0,1} => mex=2 => f(27)=2.
28: sqrt≈5.292 floor=5; fourth root≈ sqrt(5.292)=2.300, ceil=3 -> y=3,4,5 => f=2.
29: sqrt≈5.385 floor=5; fourth root≈ sqrt(5.385)=2.321, ceil=3 -> y=3,4,5 => f=2.
30: sqrt≈5.477 floor=5; fourth root≈ sqrt(5.477)=2.340, ceil=3 -> y=3,4,5 => f=2.
31: sqrt≈5.568 floor=5; fourth root≈ sqrt(5.568)=2.360, ceil=3 -> y=3,4,5 => f=2.
32: sqrt≈5.657 floor=5; fourth root≈ sqrt(5.657)=2.379, ceil=3 -> y=3,4,5 => f=2.
33: sqrt≈5.745 floor=5; fourth root≈ sqrt(5.745)=2.397, ceil=3 -> y=3,4,5 => f=2.
34: sqrt≈5.831 floor=5; fourth root≈ sqrt(5.831)=2.415, ceil=3 -> y=3,4,5 => f=2.
35: sqrt≈5.916 floor=5; fourth root≈ sqrt(5.916)=2.433, ceil=3 -> y=3,4,5 => f=2.
36: we did, f=2.
37: sqrt≈6.083 floor=6? Wait sqrt(37)=6.082, floor=6; fourth root≈ sqrt(6.082)=2.466, ceil=3? Actually 2.466 ceil=3. So y=3..6? Wait R=6. So y=3,4,5,6. f(3)=0, f(4)=1, f(5)=1, f(6)=1 => S={0,1} => f=2.
... continue until maybe 80? Let's compute x=81: sqrt=9, floor=9; fourth root=81^{0.25}= sqrt(9)=3, ceil=3. So interval [3,9]. y=3,4,5,6,7,8,9. f(3)=0, others 1 except 9=1. So S contains 0 and 1 => mex=2 => f(81)=2.
Now x=82 as computed: L=ceil(82^{1/4}) . 82^{1/4} = sqrt(sqrt(82)) ≈ sqrt(9.055)≈3.009, ceil=4; R=floor(sqrt(82))=9. y=4..9. All have f=1 (since 4-9 f=1 except 9=1). So S={1}. mex=0 => f(82)=0.
x=83: sqrt≈9.110 floor=9; fourth root≈ sqrt(9.110)=3.018, ceil=4. So same L=4,R=9 => f=0.
x=84: sqrt≈9.165 floor=9; fourth root≈ sqrt(9.165)=3.027, ceil=4 => f=0.
...
Until when does L remain 4? L= ceil(x^{1/4}). For x up to 255? Because 4^4=256. When x<256, x^{1/4}<4. So for x from 82 to 255, x^{1/4} is between about 3.01 and 3.99, ceil=4 for x>81? Actually if x^{1/4} is between 3 and 4, ceil is 4 when x^{1/4}>3. For x=81, x^{1/4}=3 exactly, ceil=3. For x=82..255, x^{1/4} in (3,4), ceil=4. So L=4 for 82 <= x <= 255. R = floor(sqrt(x)). For x up to 255, sqrt(x) <= floor(sqrt(255))=15. So R ranges from floor(sqrt(82))=9 up to 15. So for x in [82,255], interval is [4, R] with R from 9 to 15. So y from 4 to R. Which numbers in that range have what Grundy? We need to know f(y) for y from 4 up to 15. We have f(4..15)=1. Also f(16)=2, but for x with R<16, i.e., R<=15, interval does not include 16. So for x in [82, 255] with R <=15, the reachable y are only 4..R, all f=1. So S={1} => mex=0 => f(x)=0. However, if R becomes 16 or more, then the interval includes y=16 which has f=2. That would change S to include 2 as well, but still no 0. Then S would contain 1 and 2 (and maybe others) but not 0, so mex would be 0 still? Wait if S contains 1 and 2, 0 is missing, mex=0. So still 0. But if S contains 0, mex would be something else. So for x where L >=4, and interval does not include any y with f=0, then f(x)=0. However, if the interval includes some y with f=0, then f(x) might be >0. When would the interval include a y with f=0? Only if L <=3, i.e., x <=81, or if there are other numbers >3 with f=0. But we just discovered that for x in [82,255], f(x)=0. So now we have numbers >3 with f=0: specifically, 82..255 appear to have f=0. Let's verify a few: x=100: sqrt=10, floor=10; fourth root=100^{0.25}= sqrt(10)=3.162, ceil=4. So L=4,R=10, y=4..10. All f=1? Check f(10)=1, yes. So S={1} => mex=0 => f=0. x=120: sqrt≈10.954 floor=10; fourth root≈ sqrt(10.954)=3.310, ceil=4 => y=4..10 => f=0. x=150: sqrt≈12.247 floor=12; fourth root≈ sqrt(12.247)=3.5, ceil=4 => y=4..12 => all f=1 (since 4..15 are 1) => f=0. x=200: sqrt≈14.142 floor=14; fourth root≈ sqrt(14.142)=3.76, ceil=4 => y=4..14 => all f=1 => f=0. x=255: sqrt≈15.968 floor=15; fourth root≈ sqrt(15.968)=3.997, ceil=4 => y=4..15 => all f=1 => f=0. So indeed for 82..255, f=0.
Now x=256: sqrt=16, floor=16; fourth root=256^{0.25}=4 (since 4^4=256), ceil=4. So L=4, R=16. Interval y=4..16. Now f(4..15)=1, f(16)=2. So reachable Grundy values: {1,2}. Does it contain 0? No. So mex of {1,2} is 0. So f(256)=0 as well? Because 0 is missing, smallest non-negative integer not in set is 0. So f(256)=0. Check: S = {1,2}. mex = 0. So f(256)=0. But note earlier for x=256, L=4, so no 0 reachable. So f=0. So 256 also f=0.
Now x=257: sqrt≈16.031 floor=16; fourth root≈ sqrt(16.031)=4.003, ceil=5? Since 4.003 ceil = 5. So L=5, R=16. Interval y=5..16. f(5..15)=1, f(16)=2. So S={1,2} => mex=0 => f=0. So f=0 continues.
What about x where L becomes 5? That happens when x^{1/4} > 4 => x > 256. So for x in (256, 625? Actually 5^4=625, so for x from 257 to 625, L = ceil(x^{1/4}) will be 5 for x >256 up to 624? When x^{1/4} is between 4 and 5, ceil is 5 for x>256. At x=625 exactly, 625^{1/4}=5, ceil=5. So for 257 <= x <= 625, L = 5 (except maybe when x^{1/4} exactly 5, but that's fine). So L=5, R = floor(sqrt(x)). For x up to 625, sqrt up to 25. So R ranges from floor(sqrt(257)) = 16 up to 25. So interval [5, R]. Now what are f(y) for y in [5, R]? We need to know f for y up to 25. We have:
- y=5..15: f=1.
- y=16: f=2.
- y=17..? We computed 17-... up to 25? Let's compute f for 17-25: we did 17-25 all f=2? Actually we computed 17-? earlier we said f(17)=2, f(18)=2, ... up to 25? x=25 we computed f=2. Yes, so y=17..25 have f=2. Also y=24,25 are 2. So for y in 5..R (where R up to 25), the f values are: for y=5..15:1; for y=16..25:2. So S will contain 1 and 2. Does it contain 0? Only if y=1,2,3 which are less than L=5, so no. So S = {1,2} (maybe also other Grundy if some y have other values, but currently only 1 and 2). So mex is 0. Thus f(x)=0 for all x from 257 to 625 as well.
Now what about x=626? 626^{1/4} approx? 5^4=625, so 626^{1/4} slightly >5, ceil=6? Actually 5^4=625, so fourth root of 626 is about 5.000... something, but still >5? 5^4=625, so 626^{1/4} = (625+1)^{1/4} >5, but how much? Since derivative at 625: d/dx (x^{1/4}) = 1/(4 x^{3/4}) ≈ 1/(4*125)=1/500=0.002, so increase ~0.002. So about 5.002, ceil=6? Wait ceil of 5.002 is 6? No, ceil(5.002) = 6? Ceil of a number is the smallest integer ≥ that number. 5.002 is greater than 5, so the smallest integer ≥5.002 is 6? Actually 5 is less than 5.002, so not ≥. 6 is ≥5.002. Yes, ceil(5.002)=6. So L becomes 6 at x=626. But need to check exact threshold: L=6 when x^{1/4} >5, i.e., x > 5^4 =625. So for x>=626, L=6. For x up to 6^4-1 =1296-1? Actually 6^4=1296, so for x from 626 to 1295, L=6. R = floor(sqrt(x)), for x up to 1295, sqrt up to floor(sqrt(1295)) ≈ 35.98, floor=35. So interval [6, R]. Now need f(y) for y from 6 up to possibly 35. We have f(6..15)=1, f(16..? ) we have up to 25 we know are 2. But we need f for y up to 35. Let's compute f for y from 26 to 35 to see if any have f different from 1 or 2. We'll compute f for 26-35. We earlier computed 26-? we said up to maybe 35 also 2? Let's verify.
We need to compute f for numbers up to maybe 100 to see pattern. But note that for x up to 255, we determined f=0 for many. But we also need f for numbers like 26,27,... We earlier computed 27-35 as f=2? Let's recompute carefully.
We had f(16)=2, and for 17-... we computed many as 2. But we need to verify for numbers like 26-35. Let's compute systematically for each y.
But we might have missed that some numbers in [6,35] could have f=0 if they are in the range 82-255. However, 26-35 are less than 82, so they are not in the range we computed f=0. Actually we computed f for 82..255 as 0. But 26-35 are less than 82, so they are in earlier range where we had f=2? Let's compute accurately.
We'll compute f for y from 1 to maybe 300 manually or by reasoning.
We have base:
1,2,3 -> 0
4-15 -> 1
16 -> 2
But we need to check 17-81: we computed many as 2. But we should compute up to 81 to be sure.
Let's compute f for y from 17 to 81.
Recall: For each y, L_y = ceil(y^{1/4}), R_y = floor(y^{1/2}). Determine reachable z's, then mex of f(z).
We already computed up to 36 earlier.
Let's do a systematic approach with programming-like reasoning, but manually for ranges.
We can compute in ranges based on L and R.
Better: Since we only need Grundy values up to maybe 10^6? Actually max a_i is ~7.78e11, sqrt is ~8.8e5, which is about 880,000. So the maximum y we might need to compute Grundy for is up to floor(sqrt(max_ai))? Wait from a move, we go to y between x^{1/4} and x^{1/2}. So for the largest x (~7.8e11), y can be as large as sqrt(x) ~ 8.8e5, and as low as x^{1/4} ~ sqrt(sqrt(x)) ~ 915 (since 915^4 ~ 7e11? Actually 1000^4=1e12, so about 915). So the relevant y values for which we need Grundy numbers are in the range from 1 up to about 8.8e5. That's manageable! Because 880,000 is not huge. We can compute Grundy numbers for all numbers up to, say, 10^6 (1 million) using DP, by iterating over each x, and for each x, we need to iterate over y in [L,R] to collect Grundy values. However, for each x, the interval length could be up to sqrt(x) - x^{1/4}, which for x ~ 1e12 is about 1e6 - 1e3 ≈ 1e6, that's large. But if we compute for all x up to 1e6, the intervals for each x will be of varying sizes. For x up to 1e6, sqrt(x) up to 1000, and fourth root up to about (1e6)^{1/4}=100^{1/2}=10? Actually 1e6^{1/4}= (10^6)^{1/4}=10^{1.5}=31.62? Wait 10^6 = (10^3)^2, fourth root = sqrt(1000)≈31.62. So for x up to 1e6, the interval size is at most about 1000, which is manageable. But we need f up to 1e6 because the moves from the original piles can go to numbers as high as sqrt(max_ai) ≈ 8.8e5, so we need f for numbers up to about 900k. That's about 1e6 numbers, and for each we need to compute Grundy by looking at values in [L,R]. The total time might be O(N * average interval size) if naive, which could be too large (1e6 * 500 average maybe 5e8). But we can optimize using prefix sums or data structures because the interval is contiguous and we need to compute mex of set of Grundy values in a range. However, note that Grundy values might be small (like 0,1,2,... up to maybe 5 or 6). So we could compute for each x, we need to know which Grundy values appear in the interval. Since Grundy numbers are small (maybe up to 10), we can precompute for each possible Grundy value the presence in the interval using a segment tree or sliding window technique.
But there might be a more clever mathematical solution: the Grundy numbers might follow a pattern based on the number of times you can take square root until you get below certain thresholds. Actually the game is impartial and the move reduces the number drastically. We can think of "stages": numbers where the interval includes numbers from previous stage.
Given the DP up to 1e6 is feasible with careful implementation in C++? Let's consider constraints: n up to 77777, but each a_i up to 7.77e11. We need to compute Grundy for each pile. Since