We are given an interactive problem. There are n students (n even) arranged in a circle. They are numbered 1..n, with adjacency between i and i+1 (mod n). Each student receives a number (integer). The constraint: for every two adjacent students, the numbers differ exactly by 1 (absolute difference 1, but could be +1 or -1). So the sequence around the circle is such that each step changes by ±1.
We need to determine if there exists a pair of students sitting exactly across from each other with the same number. "Students with numbers i and n/2 + i sit across each other." Actually: For a circle, the person opposite to i is i + n/2 (mod n). Since indices start at 1, opposite of i is i + n/2 if i ≤ n/2, else i - n/2. So we need to find some i such that a_i == a_{i + n/2} (with indices modulo n, 1-indexed). If exists, output any such i. Otherwise output -1.
We can ask queries to get a_i for any i, up to 60 queries. n up to 100k, but only 60 queries allowed. So we need a binary search like approach.
Observations: The adjacency condition implies that the sequence is like a walk that goes +1 or -1 each step around the circle, returning to start after n steps. So it's a "closed walk" of length n on integers, step sizes ±1. It is known that for such a closed walk (since n even, parity condition: the sum of steps = 0, which is always even but more importantly, the parity of the numbers: moving each step changes parity, so after n steps, parity flips if n is odd, but n is even so parity returns to start. So it's consistent.)
Define f(i) = a_i - a_{i + n/2}. We want to know if f(i) = 0 for some i. Since indices are modulo n, consider i from 1 to n/2, define opposite pairs (i, i + n/2). There are n/2 such pairs. We need to detect a zero of f. Also note that due to adjacency constraints, f(i) might have some monotonic property? Let's explore.
Let’s consider the differences along the circle. Define d_i = a_{i+1} - a_i ∈ {+1, -1} (for i from 1 to n, with a_{n+1} = a_1). So the sequence a is determined by these steps. Then a_{i + n/2} can be expressed as a_i plus sum of steps from i to i+n/2 (mod n). Since it's a circle, we need to decide direction. Typically we go forward: from i to i+n/2, moving forward (increasing index modulo n) through n/2 steps. So let S_i = sum_{k=0}^{n/2 - 1} d_{i+k} (with indices modulo n). Then a_{i + n/2} = a_i + S_i. So f(i) = a_i - a_{i + n/2} = -S_i.
Alternatively, we could consider difference: f(i) = a_i - a_{i + n/2}. Then f(i+1) relates? Let's compute f(i+1) - f(i) = (a_{i+1} - a_{i+1+n/2}) - (a_i - a_{i+n/2}) = (a_{i+1} - a_i) - (a_{i+1+n/2} - a_{i+n/2}) = d_i - d_{i+n/2}.
Because d_i = a_{i+1} - a_i; d_{i+n/2} = a_{i+1+n/2} - a_{i+n/2}. So f(i+1) - f(i) = d_i - d_{i+n/2}. Since each d ∈ {+1,-1}, the difference d_i - d_{i+n/2} ∈ { -2, 0, 2 }. Actually possible values: if d_i = d_{i+n/2}, then 0; if d_i = 1, d_{i+n/2} = -1, then 2; if d_i = -1, d_{i+n/2}=1, then -2.
Thus f changes by steps of size 0 or ±2 when moving i to i+1. Also note that because indices modulo n, f(i + n/2) = a_{i+n/2} - a_{i+n} = a_{i+n/2} - a_i = -f(i). So f is anti-symmetric: f(i + n/2) = -f(i). In particular, if we consider i from 1 to n/2, f(i) and f(i + n/2) are negatives.
Now, what is the parity of f(i)? Since a_i and a_{i+n/2} are integers, f integer. But also from steps: a_{i+n/2} = a_i + S_i, S_i sum of n/2 steps each ±1. n/2 is integer. The parity of S_i: since each step is ±1 mod 2, it's like number of +1 steps minus number of -1 steps, but mod 2, ±1 ≡ 1 mod 2? Actually -1 mod 2 = 1 as well because both odd. So each step contributes 1 modulo 2. So S_i mod 2 ≡ (n/2) mod 2. Therefore S_i is even if n/2 is even, odd if n/2 odd. So a_{i+n/2} ≡ a_i + (n/2) (mod 2). So f(i) = -S_i, so f(i) mod 2 ≡ - (n/2) mod 2. So f(i) is even if n/2 even? Actually: if n/2 is even, S_i even => f(i) even; if n/2 odd, S_i odd => f(i) odd. So f(i) has fixed parity across all i! That's important.
Now, consider the values of f(i) as i runs from 1 to n/2 (or full circle). Because f(i+1)-f(i) ∈ { -2,0,2 }, f changes by multiples of 2. So f(i) is a sequence with constant parity (all even or all odd). Also f(i) is anti-symmetric: f(i + n/2) = -f(i). That implies that if we consider i from 1 to n/2, the f values come in pairs? Not exactly.
Also note that f(1) + f(2) + ... + f(n) = 0? Since sum a_i - a_{i+n/2} = 0? Actually each a_i appears twice: once positive, once negative? Let's compute sum_{i=1}^{n} f(i) = sum_{i} a_i - a_{i+n/2} = 0 because it's a cyclic shift. So sum of f over all n positions = 0. Since f(i + n/2) = -f(i), the sum over i=1..n/2, f(i) + f(i+n/2) = 0, so that automatically holds.
Now we need to determine if there exists i with f(i)=0. Given we can query up to 60 times, we need a binary search on f values.
Consider the function g(i) = f(i) for i=1..n/2. Or perhaps consider h(i) = a_i - a_{i+n/2} for i=1..n/2. That's n/2 values. The property: g(i+1) - g(i) = d_i - d_{i+n/2} ∈ { -2,0,2 }. So g is not necessarily monotonic, but perhaps we can examine its sign changes. Since changes are by ±2 or 0, it's like a discrete function that can go up/down. However note that g(i) and g(i+ n/2) are opposite. That might give a condition that if g(1) and g(1 + n/2) = -g(1) have opposite signs (unless zero). But that's trivial.
Alternatively, think about the product f(i) * f(i + 1)? Not sure.
Another approach: Consider the differences between opposite pairs along the circle. It's known that in such sequences, there must exist an opposite pair with equal numbers if and only if the sum of some signs is something? Actually not necessarily; examples show it may not exist (like sample 2: n=6, sequence: 1,2,3,2,1,0. Check opposite pairs: 1 vs 4? Wait indices: 1 opposite 4? n=6, n/2=3, so opposite of 1 is 4, a1=1, a4=2 -> not equal. 2 opposite 5: a2=2, a5=1 -> not equal. 3 opposite 6: a3=3, a6=0 -> not equal. So no equal opposite. So existence is not guaranteed.
We need to determine existence with few queries.
Idea: Use a kind of binary search on the circle based on the sign of f(i). Since f(i) changes slowly and f(i) and f(i+n/2) have opposite signs (unless zero), there might be a zero somewhere if the sign of f(1) and f(1+n/2) are opposite? But they are always opposite: f(1+n/2) = -f(1). So sign of f(1) and f(1+n/2) are opposite unless f(1)=0. So that doesn't help. But consider f(i) as i runs from 1 to n/2. Since f(1) and f(1+n/2) are opposite, but f(1+n/2) = -f(1). However, note that f(1) and f(1+n/2) are not consecutive; they are separated by n/2 steps.
Consider the sequence f(1), f(2), ..., f(n). Since f(i+1)-f(i) is even and bounded, and the whole sequence is antiperiodic: f(i+n/2) = -f(i). So if we look at f(i) for i=1..n, the values go around and eventually flip sign after n/2. This suggests that there might be a zero somewhere if the parity condition holds? But not necessarily, as seen in example.
We need to detect zero using queries. Since we can query at most 60 points, we need to perform a binary search on the index. But binary search on a function that might not be monotonic. However, we might be able to find a monotonic property if we consider the function f(i) as we move i, but it's not monotonic. However, perhaps we can consider the function f(i) - f(i+1) or something else.
Alternatively, there is a known solution to this problem (Codeforces problem "The Hat" maybe). I recall a problem called "The Hat" from Codeforces Round #... Indeed, there is a problem: "The Hat" interactive, with n even, adjacent differ by 1, find opposite equal numbers with ≤60 queries. The solution uses a binary search on the prefix differences or something like that. Let me recall the approach: Consider d(i) = a_i - a_{i + n/2}. Then note that d(1) and d(1 + n/2) = -d(1) are opposite signs. Then we look at d(mid) etc. Because d(i) changes by at most 2, and d(i) and d(i+1) have same parity. There's a property: if d(L) and d(R) have opposite signs, then there exists an index between them where d = 0. But is that true given the step changes of ±2? If d(L) and d(R) are integers of same parity and differ by sign (i.e., one positive, one negative) and the function can only change by steps of ±2, then there must be a zero somewhere between? Let's test: Suppose d(L) = 2, d(R) = -2. The function from L to R, each step changes by -2,0,2. Could it go from 2 to -2 without hitting 0? The smallest absolute change possible is decrement by 2: 2 -> 0 -> -2, or 2 -> -2 directly? But change of -4? Actually d(i+1)-d(i) ∈ { -2,0,2 }. So to go from 2 to -2, the net change is -4. This could be done in two steps of -2: 2 -> 0 -> -2. So if the steps are exactly -2 each time, you hit 0. But could it skip 0? For a net change of -4, you could have a step of -2 and then another step of -2: that gives 0 at the intermediate. You could also have a step of 0 then -2 then -2 etc. Any combination summing to -4 that includes only increments of -2,0,2? To get from 2 to -2 without passing through 0, you'd need a jump of -4 in one step, but that's not allowed. So indeed, if the function changes by even steps and is integer, and it goes from positive to negative, by the discrete intermediate value property, there must be some index where it is zero because the parity is constant (so zero is possible if parity matches zero? Zero has parity even. So if all values are even, zero is even. If all values are odd, zero is even, which cannot occur because all values are odd. Wait! That's crucial. f(i) has constant parity (all even or all odd). Zero is even. So if the constant parity is odd, f(i) can never be zero because zero is even. So then there is no opposite equal numbers. So the first thing to check: determine parity of f(1). If f(1) is odd, then all f(i) are odd (since changes by multiples of 2, parity preserved). Hence no zero. So we can answer ! -1.
If f(1) is even, then zero is possible. But not guaranteed. However, there is a property: Since f(i) changes by steps of ±2, and f(1) and f(1 + n/2) are opposites: f(1 + n/2) = -f(1). So if f(1) is even and non-zero, then f(1) and f(1 + n/2) have opposite signs (since one positive, the other negative). Now consider f(i) as i ranges from 1 to 1 + n/2. The start and end have opposite signs, and the function changes by steps of ±2 (even). By discrete intermediate value, there must be an index where f(i)=0. Because going from positive to negative (or negative to positive) through even steps, you must hit zero if parity allows zero. Since parity is even, zero is achievable. More formally: if f(L) > 0 and f(R) < 0 with R > L, and the function only changes by ±2 or 0, then there exists some i in [L, R] such that f(i)=0. Because the set of values reachable from f(L) by adding multiples of ±2 includes all numbers with same parity as f(L). Since f(L) positive even and f(R) negative even, there is an even number between them inclusive that is zero, and by monotonic? Wait, it's not necessarily monotonic; the function could go up and down. However, could it avoid zero? Consider f(L)=2, f(R)=-2, but the function could go from 2 to 4 to 2 to -2? That still passes through 0? Actually from 2 to 4 (increase by 2), then 4 to 2 (decrease by 2), then 2 to 0? Hmm, to go from 2 to -2 net change -4, but the path could be 2 -> 4 -> 2 -> 0 -> -2. That includes 0. Could it be 2 -> 4 -> 6 -> 4 -> 2 -> 0 -> -2? That also includes 0. Could we have a path 2 -> 2 (stay) -> 2 -> -2? That would require a jump of -4 not allowed. So any transition from 2 to -2 must at some step go from a positive (or zero) to negative? Actually, if the function only changes by ±2, then to change sign from positive to negative, you must go from 2 to 0 then to -2, or from 2 to 0 then to -2 (or from 2 to 2 then 0 then -2). In any discrete walk with step size ±2 (or 0) and starting even positive, ending even negative, the walk must take a step from 2 to 0 or from 0 to -2, but it might not necessarily visit 0? Suppose you go from 2 to 2 (0 change) many times, then eventually you take a step of -2 to go to 0? Wait 2 to -2 is not allowed, but 2 to 0 is allowed (-2). So if you want to go from 2 to -2, you must at some point go from 2 to 0 (step -2). That gives f(i)=0 at that index. More generally, for any start positive even and end negative even, the net change is negative even number. Since steps are ±2, you will at some point cross from a positive value to a non-positive value. The smallest positive even value is 2. To go to a negative value, you must at some step go from 2 to 0 (if you go from 2 to -2 directly, that would be step -4, not allowed). If you go from 4 to 2, that's still positive; then from 2 to 0 is required to become non-positive. So you must hit 0 exactly when stepping from 2 to 0, or possibly from 0 to -2? But if you go from 2 to 0, that's hitting 0. So yes, if the parity is even, then there must be an index where f(i)=0. But is it guaranteed that there is an i such that f(i)=0 if f(1) is even? Let's check the counterexample: In the sample 2, n=6, we have a: 1,2,3,2,1,0. Compute f(1) = a1 - a4 = 1-2 = -1 (odd). So f(1) odd => no zero. So that matches the parity condition. So indeed, if f(1) is odd, answer is -1. But what about a case where f(1) even but no zero? Could that happen? Let's try to construct. n=4 maybe? n=4, sequence must satisfy adjacent diff 1. Let's try: a1=0, a2=1, a3=2, a4=1? Check adjacency: 0->1 diff1, 1->2 diff1, 2->1 diff1, 1->0 diff1? a4 to a1: 1 to 0 diff1. So valid. Opposite pairs: 1 opposite 3: a1=0, a3=2 -> f(1) = -2 (even). 2 opposite 4: a2=1, a4=1 -> f(2)=0. So there is a zero. Another: a1=0,a2=1,a3=0,a4=1? Check: 0->1 diff1,1->0 diff1,0->1 diff1,1->0 diff1. Opposite: 1-3:0-0=0, so zero. What about n=4 but no zero? Try a1=1,a2=2,a3=3,a4=2: 1-2 diff1,2-3 diff1,3-2 diff1,2-1? a4 to a1:2-1=1 ok. f(1)=1-3=-2 (even). f(2)=2-2=0 so zero exists. Actually any n=4? Let's search systematically. n=4, the sequence of steps d1,d2,d3,d4 each ±1, sum of steps must be 0 mod? Actually the condition for closed walk: total sum of steps around circle must be 0 because a1 = a1 + sum(d_i) => sum(d_i)=0. For n=4, we need sum(d1+d2+d3+d4)=0. With each ±1, possible sums: 0, ±2, ±4? But must be 0 for consistency, because returning to start after 4 steps: a1 = a1 + total sum => total sum = 0. So sum(d_i)=0. That implies number of +1 equals number of -1, so two of each. Then f(1)=a1 - a3 = -(d1+d2). Because a3 = a1 + d1 + d2. So f(1) = - (d1+d2). Since d1,d2 are ±1, sum could be -2,0,2. So f(1) ∈ { -2,0,2 } all even. So parity even always. And we have f(2)=a2-a4 = -(d2+d3). Could be even. So there might be no zero if both f(1) and f(2) are non-zero and of same sign? But f(2) is not independent because sum(d_i)=0. Let's enumerate possibilities: d1,d2,d3,d4 with two +, two - and sum 0. Cases: (++--) up to permutations. Compute f(1) = -(d1+d2). If d1+d2=0, then f(1)=0. If d1+d2=2 => f(1)=-2; then d3+d4 must be -2 because total sum 0. Then f(2)= -(d2+d3). Could that be non-zero and never zero? Example: choose d1=1,d2=1 => d1+d2=2 => f(1)=-2. Need d3=-1,d4=-1. Then d2+d3 = 1 + (-1) = 0 => f(2)=0. So zero exists. Another: d1=1,d2=-1 => sum0 => f(1)=0. Another: d1=-1,d2=1 => f(1)=0. Another: d1=-1,d2=-1 => f(1)=2? Actually f(1)= -(-2)=2? Wait d1+d2=-2 => f(1)= -(-2)=2. Then d3=1,d4=1. Then d2+d3 = -1+1=0 => f(2)=0. So indeed zero always appears. So for n=4, always exists zero. For n=6, we saw possible no zero (with parity odd). Let's test n=6 with even parity: Is there a case with f(1) even and no zero? Let's attempt to construct. n=6, steps d1..d6 sum 0. f(i) = a_i - a_{i+3} = - (d_i + d_{i+1} + d_{i+2}) (since three steps). So f(i) = - sum of three consecutive d's. Since each d ±1, sum of three can be -3,-1,1,3. So parity: if sum is odd ( -3, -1, 1, 3 ) => f(i) opposite sign, so f(i) = -odd = odd. So f(i) odd always? Actually f(i) = - (odd) = odd? Negative of odd is odd. So f(i) is odd for n=6? Because sum of three ±1's is odd always (since three odd numbers sum is odd). Indeed, any odd number of ±1's sum is odd. So for n/2 odd, f(i) is odd. For n/2 even, f(i) is even. So parity of f(i) is determined solely by n/2 parity: if n/2 is even, f(i) even; if n/2 odd, f(i) odd. Let's verify: n=4, n/2=2 even -> f even. n=6, n/2=3 odd -> f odd. n=8, n/2=4 even -> f even. So indeed, parity is fixed: f(i) parity = (n/2) mod 2 *? Actually f(i) = -S_i, S_i sum of n/2 steps each ±1 => S_i parity = n/2 mod 2. So f(i) parity = n/2 mod 2. So if n/2 is even, f(i) even; if n/2 odd, f(i) odd. Therefore, the existence condition reduces to: if n/2 is odd, f(i) always odd, so zero (which is even) cannot occur => answer always -1. But wait, the sample 1 with n=8 has n/2=4 even, so zero possible. And sample 2 n=6 has n/2=3 odd, so impossible. But is it always impossible when n/2 is odd? Let's check with n=2: n/2=1 odd. Then there are only 2 students, opposite? With n=2, opposite pairs are just each other? Actually for n=2, opposite of 1 is 2? Since n/2=1, i opposite i+1? Then we need a1 = a2? Given adjacency diff 1 (since they are adjacent and also opposite). But adjacency requires |a1 - a2| = 1. So cannot have a1 = a2. So indeed impossible. For n=6, we saw example with no zero. Could there be a zero when n/2 odd? Suppose n=6, f(i) must be odd. Zero is even, so cannot be zero. So indeed, if n/2 is odd, answer is always -1. So we don't need any queries? Wait, but we must be careful: The parity argument says f(i) = - sum of n/2 steps. Each step is ±1. The sum of n/2 numbers each odd (1 mod 2) is congruent to n/2 mod 2. So if n/2 is odd, sum is odd, so f(i) is odd. Therefore f(i) cannot be 0. So answer is always -1. So for n/2 odd, we can directly output "! -1" without any queries. But is this always valid? Let's double-check with a potential counterexample: n=10, n/2=5 odd. Then f(i) = a_i - a_{i+5}. Sum of 5 steps, each odd, total odd => f(i) odd. So cannot be 0. So indeed always -1. But is there any possibility that the adjacency condition could be violated? No, it's given. So yes, if n/2 is odd, no opposite equal numbers. So we can answer immediately.
Thus, the problem reduces to the case where n/2 is even. Then f(i) is even for all i. Moreover, we have the property that f(1) and f(1 + n/2) are opposites, i.e., f(1 + n/2) = -f(1). Since f(1) is even, if f(1)=0 we are done. Otherwise, f(1) and f(1 + n/2) have opposite signs. Then by the discrete intermediate value property (since f changes by steps of ±2), there must exist some i between 1 and 1 + n/2 inclusive such that f(i)=0. Actually, inclusive of endpoints? f(1) non-zero, f(1+n/2) = -f(1) non-zero opposite sign. Since we have a path from positive to negative (or negative to positive) with steps ±2, we must hit zero somewhere. This is a well-known argument in the Codeforces problem.
Therefore, in the case n/2 even, there always exists a solution? Wait, is it guaranteed? Let's test with n=8, which is n/2=4 even. The sample 1 had a solution. Could there be a case with n=8 and no opposite equal numbers? Let's try to construct: need sequence of length 8 with adjacent diff 1, and for all i, a_i ≠ a_{i+4}. f(i) = a_i - a_{i+4} must be non-zero even numbers. Since f(1) and f(5) opposite signs, that's possible. But we argued that if f(1) ≠ 0, then because f(1) and f(5) have opposite signs, there must be some i where f(i)=0. Is that always true? Let's test with a concrete example. Let's try to design a sequence for n=8 such that f(i) never zero. Let's compute f(i) in terms of steps. For n=8, f(i) = -(d_i + d_{i+1} + d_{i+2} + d_{i+3}) (4 steps). So f(i) is even (since sum of 4 ±1 is even). We want all f(i) ≠ 0, i.e., each sum of four consecutive d's is never 0. And we need sum of all 8 d's = 0 (closed condition). Also f(1) and f(5) are opposites because f(5) = -f(1) (since f(i+4) = -f(i)). So if f(1) ≠ 0, then f(5) = -f(1) opposite sign. Now consider the sequence f(1), f(2), f(3), f(4), f(5). The step changes: f(i+1)-f(i) = d_i - d_{i+4}. Since d_i and d_{i+4} are ±1, the difference is in { -2,0,2 }. We want to avoid 0. Could we have f(1)=2, f(2)=2, f(3)=2, f(4)=2, f(5)=-2? That would require changes: f(2)-f(1)=0 => d1-d5=0 => d1=d5. f(3)-f(2)=0 => d2=d6. f(4)-f(3)=0 => d3=d7. f(5)-f(4) = -4? Actually from 2 to -2 is -4, but allowed change is ±2 or 0, not -4. So impossible. So we need intermediate steps that go gradually down. Could we go 2, 2, 0, -2? But 0 is prohibited. So maybe 2, 0, -2, -2? But 0 again. 2, 0, 0, -2? zero. 2, 2, 2, 0? zero. Let's try to see if we can have f(1)=2, f(2)=4, f(3)=2, f(4)=0? zero. Hmm. Let's attempt systematically: Represent d_i. We need sum of 4 consecutive non-zero. Suppose we try to assign d_i such that all sums of four consecutive are either 2 or -2 or 4 or -4? But must be even and non-zero. f(i) = - sum, so sign reversed. So we want sums all non-zero even. And we also have f(i+4) = -f(i) so sums for i and i+4 are opposite. So the sequence of sums (S_i = sum of four d's) must have alternating signs? Not necessarily alternating, but S_{i+4} = -S_i. So for i=1..4, S_i can be positive or negative, but then S_{i+4} is opposite. Now consider the differences: S_{i+1} - S_i = (d_{i+1}+d_{i+2}+d_{i+3}+d_{i+4}) - (d_i+d_{i+1}+d_{i+2}+d_{i+3}) = d_{i+4} - d_i. So S_{i+1} = S_i + (d_{i+4} - d_i). Since d_{i+4} - d_i ∈ { -2,0,2 }. So indeed S changes by ±2 or 0. And S_i must be even (since 4 steps). Starting from S_1 = s (non-zero even). After 4 steps, we need S_5 = -s. That means net change from i=1 to i=5 (i.e., over 4 steps) is -2s. But the total change over 4 steps is sum_{i=1}^{4} (d_{i+4} - d_i) = (d_5 - d_1)+(d_6-d_2)+(d_7-d_3)+(d_8-d_4). This sum must equal -2s. Also note that d_5,d_6,d_7,d_8 are the second half. But we can try to construct.
Let's try to find explicit values. Let n=8. Let d1..d8 each ±1, sum zero. We need S_i ≠ 0 for i=1..8. Compute S1 = d1+d2+d3+d4. Since sum of all d =0, we have d5+d6+d7+d8 = -S1. So S5 = d5+d6+d7+d8 = -S1, which matches condition. So S1 non-zero even. For S2 = d2+d3+d4+d5 = S1 - d1 + d5. So S2 = S1 + (d5 - d1). Since d5 - d1 ∈ { -2,0,2 }, S2 = S1, S1±2. Similarly S3 = S2 + (d6 - d2), etc. We want to avoid zero. Starting with S1 = 2 (or -2). Could we have a path from 2 to -2 in 4 steps without hitting 0? That would require net change -4. Over 4 steps, each step change ±2 or 0. Sum of changes = -4. The only way to get sum -4 with steps in { -2,0,2 } is to have at least two -2 steps and the rest zero or something. But if we have two -2 steps and two 0 steps, the path would be: step1: 2 -> 0 (if -2), step2: 0 -> -2 (if -2), then step3: -2 -> -2 (0), step4: -2 -> -2 (0). That path hits 0. If we try to avoid 0, we might try to go from 2 to 4 to 2 to 0? That hits 0. Could we go 2 -> 4 -> 2 -> -2? That goes 2 to 4 (+2), 4 to 2 (-2) back to 2, then 2 to -2? But 2 to -2 would be -4 step, not allowed. So you need intermediate steps. So seems impossible to go from 2 to -2 in steps of ±2 without hitting 0 because the only way to decrease by 4 is to pass through 0 (since from an even number, decreasing by 2 repeatedly will eventually hit 0 if you go from positive to negative). More formally, if you have a sequence of integers with same parity, starting positive even, ending negative even, then by the discrete intermediate value theorem, there must be a zero because the set of values includes all numbers of that parity between start and end if the sequence is continuous? But here it's not required to take every integer, but it can skip some? Since steps are ±2, you can skip odd numbers, but you can also skip some evens? Let's list possible evens: ..., -4, -2, 0, 2, 4, ... Starting at 2, a step of -2 gives 0. So if you ever take a step -2 from 2, you land on 0. If you take step +2, you go to 4. From 4, you can take -2 to go to 2, or -2 again to 2? Actually 4 to 2 is -2, that's 2 (positive). To eventually become negative, you need to go from positive to non-positive. The only positive numbers reachable are even positive: 2,4,6,... To become negative, you must at some point have a transition from a positive number to a non-positive number. The smallest positive even is 2. From 2, the only possible moves are -2 to 0, or +2 to 4. So if you are at 2 and you want to become negative, you must eventually take a -2 step from 2, which lands at 0. If you are at 4, you could go 4 -> 2 (by -2), then eventually from 2 to 0. Or you could go 4 -> 6 -> ... but eventually you need to come down. The only way to get to a negative number is to pass through 0, because you cannot jump from a positive even (≥2) to a negative even (≤ -2) in one step (since step magnitude is 2). So you must at some point be at 0. Thus, indeed, if start positive even and end negative even, there must be some index where f(i)=0. Similarly if start negative even and end positive even.
Therefore, if n/2 is even, then f(1) and f(1+n/2) have opposite signs (provided f(1) != 0). Since f(1) is even, they are both even. So a zero must exist between them. Hence, in that case, there is always at least one solution. So answer always exists when n/2 even? Wait, is it possible that f(1)=0? Then solution exists trivially. So indeed, for n/2 even, there is always at least one i such that a_i = a_{i+n/2}. So the answer always exists. But is that true? Let's test with a potential n=8 counterexample. Try to construct a sequence where all f(i) ≠ 0. According to our reasoning, it's impossible. Let's try to brute mental search: n=8, we need sum of all d's = 0. Let's attempt d = [1,1,1,-1,-1,-1,1,-1]? Sum: 1+1+1-1-1-1+1-1 =0? Compute: 1+1=2, +1=3, -1=2, -1=1, -1=0, +1=1, -1=0. So sum 0. Compute S1 = d1+d2+d3+d4 = 1+1+1-1=2 => f(1) = -2 (non-zero). S2 = d2+d3+d4+d5 = 1+1-1-1=0 => f(2)=0. So zero appears. Try another: d = [1,1,-1,1,-1,-1,1,-1]? Sum: 1+1-1+1-1-1+1-1 =0? 1+1=2, -1=1, +1=2, -1=1, -1=0, +1=1, -1=0 => yes. S1 = 1+1-1+1 =2 => f1=-2. S2 = 1-1+1-1 =0 => zero. Try d = [1,1,1,1,-1,-1,-1,-1]? Sum 0. S1 = 4 => f1=-4, S2 = 1+1+1-1=2 => f2=-2, S3 = 1+1-1-1=0 => zero. Try d = [1,1,1,-1,1,-1,-1,-1]? sum: 1+1+1-1+1-1-1-1=0? 3-1=2, +1=3, -1=2, -1=1, -1=0 => ok. S1 = 1+1+1-1=2 -> f1=-2, S2=1+1-1+1=2 -> f2=-2, S3=1-1+1-1=0 -> zero. So seems zero always appears. I'm convinced.
Thus, the problem simplifies: For given n even, if n/2 is odd, answer is always -1. If n/2 is even, answer always exists. So we just need to find some i with a_i = a_{i+n/2}. But we still need to output such i. And we have limit of 60 queries. So we must find the index with zero f(i) using limited queries. Since we know existence guaranteed (if n/2 even), we can use a binary search-like approach to locate a zero of f(i). Note that f(i) is defined for i=1..n (or i=1..n/2). But we can query values a_i and a_{i+n/2} to compute f(i). We can query at most 60 times. n up to 100k, so binary search is feasible: we need about log2(n) ≈ 17 queries for n=100k, well within 60. However, f(i) is not monotonic, but we can use a technique similar to binary search on a function that changes sign and is discrete, ensuring existence of zero between two indices with opposite signs.
Specifically, define g(i) = a_i - a_{i+n/2} for i from 1 to n/2. Actually, g(i) for i > n/2 can be derived from g(i - n/2) = -g(i), but we can consider i in range [1, n/2]. We know that g(1) and g(1 + n/2) are opposite signs, but 1+n/2 > n/2. However, we can compute g(1) and g(1 + n/2) by querying a1, a_{1+n/2}, and also a_{1+n/2} and a_{1+n/2 + n/2}=a_{1+n}=a_1? Actually careful: For i in 1..n, define F(i) = a_i - a_{i+n/2} where indices mod n (1-indexed). Then F(i) for i=1..n/2 yields values, and F(i + n/2) = -F(i). So if we compute F(1), we automatically know F(1+n/2) = -F(1) without query? Actually we need to know a_{1+n/2} and a_{1+n} (which is a1) to compute F(1+n/2) = a_{1+n/2} - a_{1+n} = a_{1+n/2} - a1 = -F(1). So indeed if we compute F(1), we know F(1+n/2) as its negative. So we don't need extra queries. But we need to verify that F(1) is even? We'll compute it by querying a1 and a_{1+n/2}. That's two queries. But we can also compute F(1) using one query if we later query both? Actually to compute F(1) we need both a1 and a_{1+n/2}, so that's two queries. But we might need to query only a few.
Now, we know that F(1) and F(1 + n/2) have opposite signs unless zero. Since existence guaranteed, if F(1) != 0, then there exists some i between 1 and 1+n/2 where F(i)=0. However, note that i could be > n/2? But because of periodicity, if i > n/2, then F(i)=0 => also F(i - n/2)=0? Because F(i)=0 implies a_i = a_{i+n/2}. If i > n/2, then i - n/2 is in 1..n/2, and then a_{i-n/2} = a_i? Actually careful: If F(i)=0, then a_i = a_{i+n/2}. For i > n/2, define j = i - n/2, then a_j = a_{j+n/2}? Because a_{j+n/2} = a_i? Not exactly: j = i - n/2, then a_j = a_{i - n/2}, and a_{j+n/2} = a_i. Since a_i = a_{i+n/2}, we don't have direct equality between a_j and a_{j+n/2}. However, we can note that if F(i)=0, then also F(i - n/2) = a_{i-n/2} - a_i. That's not necessarily zero. So zeros don't necessarily translate. Better to restrict to i in 1..n/2. Because there are exactly n/2 opposite pairs. For each pair (i, i+n/2), we can check equality. So we want to find i in [1, n/2] such that a_i = a_{i+n/2}. That's equivalent to F(i)=0 for i in [1, n/2]. So we need to find zero in that range. What about the range [1, n/2]? We know F(1) and F(1+n/2) are opposite signs, but 1+n/2 is not in [1, n/2] unless n/2 < 1? Actually n/2 >=1, so 1+n/2 > n/2 for n>2. So we cannot directly use opposite signs within [1, n/2] endpoints. However, we can consider the function on the circle: Since F(i+n/2) = -F(i), if we look at i from 1 to n/2, then F(1) and F(n/2+1) are opposites. But n/2+1 is just beyond the range. But we can consider the interval from 1 to n/2+1 inclusive, and there will be a zero. That zero might be at the endpoint n/2+1, which corresponds to pair (n/2+1, n+1) = (n/2+1, 1). That's a pair across as well? Actually (n/2+1) and (1) are opposite because 1 is opposite n/2+1? Let's check: Opposite of 1 is 1+n/2. So if i = n/2+1, its opposite is n/2+1 + n/2 = n+1 ≡ 1 mod n. So yes, the pair (n/2+1, 1) is an opposite pair. So i = n/2+1 is essentially the same as i=1? Actually careful: The pairs are unordered; the opposite pairs are (i, i+n/2) for i=1..n. For i = n/2+1, the opposite is i+n/2 = n+1 ≡ 1, so the pair is (n/2+1, 1). This is a valid opposite pair. However, our range [1, n/2] only includes i where the smaller index is ≤ n/2. But the pair (n/2+1, 1) is the same as the pair (1, n/2+1) if we consider i=1. Because opposite of 1 is n/2+1. So equality a_1 = a_{n/2+1} is checked when i=1. So the zero at i = n/2+1 corresponds to the same equality as i=1? Actually if F(n/2+1)=0, that means a_{n/2+1} = a_{n/2+1+n/2} = a_{n+1}=a_1. So it implies a_1 = a_{n/2+1}. That's exactly the same condition as F(1)=0 because F(1)=a_1 - a_{1+n/2}=a_1 - a_{n/2+1}. So F(1)=0 and F(n/2+1)=0 are equivalent. So zeros come in pairs (i and i+n/2) meaning the same equality. So it's sufficient to search i in [1, n/2] for zero.
Thus, we need to find an i in [1, n/2] with F(i)=0. How to binary search? The key property: Consider the function F(i) for i=1..n/2. We know F(1) and F(n/2+1) are opposites, but F(n/2+1) is not in the domain. However, consider the values of F at i and i+1. Not monotonic. But we can adapt using the following idea: Look at the function for i in [1, n/2]. Define another function H(i) = F(i) maybe? But we need two indices with opposite signs to apply binary search. Since we know existence, we can try to find such a pair (L,R) within [1, n/2] such that F(L) and F(R) have opposite signs. Then we can binary search because the function, though not monotonic, still must have a zero between L and R due to the step changes? Let's verify: If we have two indices L < R within 1..n/2 such that F(L) and F(R) have opposite signs, does it guarantee a zero somewhere between? Not necessarily, because the function could jump over zero without hitting it if the step size were >1? But step changes are ±2, and values have same parity. Suppose F(L)=2, F(R)=-2, and there is a sequence of steps between them. As argued earlier, to go from 2 to -2, you must at some point be at 0 because you need to decrease by 4 via steps of ±2, which forces passing through 0. However, could the path go from 2 to 4 to 2 to 4 ... and then directly to -2? The direct step from 2 to -2 is -4, not allowed. From 4 to -2 is -6, not allowed. So the only way to change sign is to pass through 0. So yes, if you have opposite signs, there must be a zero somewhere between (including possibly at an index where F(i)=0). This holds as long as the function changes by steps of ±2 and the parity is constant (even). Since when n/2 even, F(i) is even. So opposite signs imply zero exists in the interval (including possibly at boundaries if one is zero). So we can binary search if we can find L,R with opposite signs.
Now, can we get such L,R? We know F(1) and F(1+n/2) are opposite signs, but 1+n/2 is not in [1, n/2]. However, we can map it back: F(1+n/2) = -F(1). But that's not directly an index in [1, n/2] unless we consider modulo. But note that F(1+n/2) corresponds to the pair (1+n/2, 1). That's essentially the same as the pair (1,1+n/2). So if F(1) != 0, then F(1) and F(1) have opposite signs? No. We need two distinct indices within the range. But we can use the fact that F(1) and F( n/2 ) maybe have opposite signs? Not guaranteed.
Maybe better: Consider the function over i from 1 to n/2+1 (inclusive). Then we have F(1) and F(n/2+1) opposite signs. And both endpoints are valid queries (though n/2+1 is not ≤ n/2, but we can still query F(n/2+1) because we can compute a_{n/2+1} and a_{n+1}=a_1). That's just one extra query: we need a_{n/2+1}. But we can treat the interval of indices from 1 to n/2+1 (size n/2+1). The zero we seek might be at an interior index or at endpoints. Since F(1) and F(n/2+1) are opposites, there must be some i in [1, n/2+1] where F(i)=0. Moreover, because F(i) and F(i+n/2) are negatives, the zero could be at i = n/2+1, which corresponds to F(1)=0 as well? Actually if F(n/2+1)=0, then a_{n/2+1}=a_1, meaning F(1)=0. So it's the same condition. So effectively, we are searching for i in [1, n/2] such that F(i)=0. So we could binary search on the circle from 1 to n/2+1, but careful with indices.
A standard solution from CF: For n/2 even, we know there exists a solution. They query a1 and a_{1+n/2} to get d1 = a1 - a_{1+n/2}. If d1=0, output. Otherwise, we know d1 is even and non-zero. Then they perform a binary search on the segment [1, 1 + n/2] (i.e., length n/2). At each step, they pick mid = (L+R)//2, query a_mid and a_{mid + n/2} (adjust modulo). Compute d_mid = a_mid - a_{mid+n/2}. If zero, done. Otherwise, they look at the sign of d_mid relative to d_L. Since d changes by steps of ±2, the sign pattern is such that if d_L and d_mid have the same sign, then there is a zero between mid and R; else between L and mid. Something like that. More precisely, they use the property that the function d(i) is "monotonic" in the sense that d(L) * d(mid) >=0 indicates something about the zero crossing. Actually we need to recall exact logic.
Let's define for i from 1 to 1 + n/2, d(i) = a_i - a_{i+n/2}. This function satisfies: d(i+1) - d(i) = (a_{i+1} - a_{i+1+n/2}) - (a_i - a_{i+n/2}) = (a_{i+1} - a_i) - (a_{i+1+n/2} - a_{i+n/2}) = s_i - s_{i+n/2}, where s_i = a_{i+1} - a_i = ±1. So difference is either -2,0,2. So it can go up/down but only by even steps. Also d(1 + n/2) = -d(1).
Now suppose d(1) > 0 (even positive). Then d(1 + n/2) = -d(1) < 0. So there is a change of sign over the interval. Now consider any midpoint m. We have d(m). The sign of d(m) relative to d(1) tells us where the zero crossing occurs. If d(m) has the same sign as d(1) (i.e., >0), then since d(m) >0 and d(1+n/2) <0, the zero must be to the right of m (i.e., between m and 1+n/2). Conversely, if d(m) <0, then the zero must be between 1 and m (since d(1)>0 and d(m)<0). This is exactly like binary search on a function that changes sign, provided the function is guaranteed to have exactly one sign change? But there could be multiple zeros. However, as long as there exists at least one zero, the sign change from start to end ensures that for any subinterval, if the endpoints have opposite signs, there is a zero inside. So we can maintain an interval [L,R] such that d(L) and d(R) have opposite signs (or one zero). Initially, L=1, R=1+n/2, with d(L) and d(R) opposite (since d(R) = -d(L) and d(L) !=0). Then for binary search, we pick mid = (L+R)//2. Query d(mid). If zero, output. Else, if sign(d(mid)) == sign(d(L)), then the sign change is between mid and R, so set L = mid. Else set R = mid. Continue until we find zero. Since the interval length halves each time, we will converge to an index where d(i)=0 in O(log n) steps. However, we must ensure that mid is always within [1, 1+n/2] and that we can compute d(mid) by querying two numbers: a_mid and a_{mid + n/2}. Note that mid + n/2 may exceed n, but we take modulo n (with wrap-around, but careful: indices are 1..n. If mid + n/2 > n, then we subtract n: equivalent index = mid + n/2 - n). Because a_{mid+n/2} means the student opposite mid. Since n is even, the opposite of i is i + n/2 if i ≤ n/2, else i - n/2. For i in [1, n], the opposite is ((i-1 + n/2) mod n) + 1. So for mid ∈ [1, n], we can compute opposite index as mid + n/2 if mid ≤ n/2, else mid - n/2. But note that in our binary search interval, we start L=1, R=1+n/2. Here R can be > n? Since 1+n/2 ≤ n? Actually n is even, so n/2 ≤ n. 1+n/2 ≤ n? For n=8, 1+4=5 ≤8. For n=100000, 1+50000=50001 ≤ 100000. So R is ≤ n? Because n/2 + 1 ≤ n for n≥2? n/2+1 <= n when n/2+1 <= n => 1 <= n/2, true for n>=2. So R is within [1,n]. So both L and R are valid indices from 1 to n. Good. So mid will also be in [1, n] since L,R integers in that range. So we can query a_mid and a_{mid+n/2}? But careful: mid could be > n/2? For example, if L=1, R=5 (n=8), mid=3 (≤4), fine. But as we narrow, L and R could both be > n/2? Possibly. But then mid + n/2 might exceed n? Let's examine: For any mid between 1 and n, the opposite index is defined as (mid + n/2) mod n (with wrap). In the problem, student i and i+n/2 (mod n) are across. So we need to compute opposite index correctly. If mid <= n/2, opposite = mid + n/2. If mid > n/2, opposite = mid - n/2. So we should compute opposite index as: opp = mid + n/2; if opp > n then opp -= n. That works because mid in 1..n, n/2 ≤ n, opp ∈ [1+n/2, n+n/2], subtract n if > n gives opp ∈ [1, n/2]? Actually if mid > n/2, then mid + n/2 > n, so opp = mid + n/2 - n = mid - n/2, which is ≤ n/2? Since mid ≤ n, mid - n/2 ≤ n/2. So it's valid. So we can compute opp accordingly. So we can query a_mid and a_opp. That yields d(mid) = a_mid - a_opp.
Now, note that our interval [L,R] originally is [1, 1+n/2]. Both ends inclusive? But we need to ensure that the property of opposite signs holds for L and R throughout. At start, d(L) and d(R) have opposite signs (since d(R) = -d(L)). But after we update L or R to mid, do we maintain that d(L) and d(R) have opposite signs? Let's analyze. Suppose d(L) >0, d(R) <0. We pick mid, compute d(mid). If d(mid) >0, then we set L=mid. Now new L has d(L)>0, and R still has d(R)<0, so opposite signs. If d(mid) <0, set R=mid, then L has d(L)>0, R has d(R)<0, opposite signs. So property holds. Also note that d(L) and d(R) are never zero because we would have stopped. So we can continue.
Now, what about the length of the interval? Initially, length = n/2. After each step, we approximately halve the length. But note that the indices are integer, and we maintain that L < R? Since we set L=mid or R=mid. Usually binary search uses while (R - L > 1) because if they become adjacent, then the zero must be at either L or R? Actually with sign change, if L and R are consecutive integers, then we might need to check both. Since step changes by ±2, if d(L) and d(R) have opposite signs and R = L+1, then there must be a zero at either L or R? Let's test: Suppose d(L)=2, d(L+1) = ? Could it be -2? Then d(L) and d(L+1) opposite. Could there be no zero? But if d(L)=2, d(L+1)=-2, then difference is -4, but allowed step is ±2,0. -4 is not allowed. So that cannot happen. The only possible values: from 2, the next can be 0,2,4. So if d(L)=2, d(L+1) could be 0,2,4. If it's 0, then we have zero. If it's 2, same sign. If it's 4, same sign (positive). So it cannot be negative if L+1 is next. To get negative, you need to pass through 0. Therefore, if d(L) > 0 and d(L+1) < 0, it's impossible because you'd have to go from positive to negative in one step, which would require stepping over 0 but step size only 2. The only way to have opposite signs at adjacent indices is if one of them is 0. So indeed, if L and R become consecutive and d(L) and d(R) have opposite signs, then either d(L)=0 or d(R)=0. So we can detect zero when interval length becomes 1? Actually if we maintain L and R such that d(L) and d(R) opposite, and we narrow until R = L+1, then we can conclude that either d(L)=0 or d(R)=0? Let's examine: Suppose d(L) > 0, d(R) < 0, and R = L+1. Could both be non-zero? If d(L) = 2, then d(R) must be either 0,2,4,... but negative? No. So impossible. Hence at least one is zero. But could both be zero? Possibly if both zero (but that would mean both are zero). So we can check both. In binary search, we can stop when we find zero or when interval reduces to small size and then query both endpoints. But we can also continue until we explicitly hit zero.
Given that we have at most 60 queries, and n up to 100k, log2(100k) ~ 17. So we can easily afford binary search, including initial queries. We need to query a1 and a_{1+n/2} to compute d(1). That's 2 queries. Then each binary search step queries a_mid and a_{mid+n/2} (2 queries). But we can possibly reuse previously queried values? But not necessary, 2 per step is okay: 2*log2(50000) ~ 34 queries, total ~36, well within 60. Actually note: we could compute d(mid) with one query if we already know one of the values? Unlikely. So we can just use 2 per step. But we might reduce to 1 per step by using a trick: For mid, we query only a_mid, and we can infer a_{mid+n/2} from previously known? Not reliable. So 2 per step is safe.
But wait, we also might need to query a_{1+n/2} initially. That's one query for a1? Actually we need both a1 and a_{1+n/2}. So that's 2 queries. Then each mid requires 2 queries. So total queries = 2 + 2 * number_of_steps. Steps: binary search on interval of length n/2 (which is up to 50000). log2(50000) ≈ 16. So total ≈ 34. That's within 60.
But we can also reduce to 1 query per step by noting that we already queried some indices? Not needed.
However, we should also consider the case when n/2 is even: we must output some i. We can perform binary search as described to find a zero. Since we are guaranteed existence, we will eventually find one.
But is it always true that d(L) and d(R) will maintain opposite signs after updating? Yes, as argued. However, one nuance: when we set L=mid or R=mid, we must ensure that the new interval still contains a zero. That's guaranteed because if d(L) and d(mid) have same sign, then the sign change is between mid and R, so zero is in (mid,R]; similarly if opposite, zero is in [L,mid). So it's fine.
Now, there is an extra detail: The function d(i) is defined for i=1..n, but we are using i up to 1+n/2. However, note that when we compute d(mid) for mid > n/2, we must compute opposite index correctly. But that's fine.
But we must also consider that the opposite index of mid might be less than or equal to n. That's okay.
Thus algorithm:
- Read n.
- If (n/2) % 2 == 1 (i.e., n/2 odd), then answer is always -1. But wait, do we need to confirm with any query? The problem statement says: "not all numbers appeared exactly twice." But that doesn't affect existence. According to our parity reasoning, when n/2 is odd, f(i) is odd for all i, so can't be zero. So we can output "! -1" without any queries. But is it always correct? Let's verify thoroughly.
Given: For each adjacent, numbers differ exactly by one. So a_{i+1} = a_i ± 1. Then consider a_i - a_{i+n/2} =? As derived, a_{i+n/2} = a_i + sum_{k=0}^{n/2-1} d_{i+k}. Each d_{i+k} is ±1. Sum of an odd number of odd numbers is odd. Therefore, a_{i+n/2} - a_i is odd. So a_i - a_{i+n/2} is odd. So it cannot be zero. Hence, indeed no opposite equal numbers. So answer is -1. So we can output immediately.
But wait, the sample input 2 had n=6 (n/2=3 odd) and they queried all 6 positions and then output -1. They didn't have to query all; they could have output -1 directly. But the problem allowed up to 60 queries, but they used 6. But we can do better.
Thus, for n/2 odd, output "! -1" without any queries.
- Else (n/2 even). Then we know there exists a pair. We'll find it via binary search.
Procedure:
- Let L = 1, R = 1 + n/2 (since n/2 even, note that n/2 is integer, so R is within [1,n]).
- Query a[L] and a[opposite(L)] to compute dL. Actually we need d(L) and d(R). But we can compute d(L) by querying a_L and a_{L+n/2} (or opposite). Similarly for d(R). However, note that d(R) = a_R - a_{R+n/2}. But R = 1 + n/2, so R + n/2 = 1 + n = 1 mod n? Actually 1 + n/2 + n/2 = 1 + n, which modulo n is 1. So opposite of R is 1. So d(R) = a_R - a_1. So if we have a_1 already from query for L, we only need a_R to compute d(R). So we could query a_1, a_{1+n/2}, and a_R? That might be 3 queries initially. But we can also compute d(R) as -d(L) if we already have d(L) because d(R) = -d(L) from the anti-symmetry? Let's check: d(R) = a_R - a_{R+n/2} = a_{1+n/2} - a_1 = -(a_1 - a_{1+n/2}) = -d(1). So indeed, d(R) = -d(L) always, regardless of values, as long as we define d(i) = a_i - a_{i+n/2}. Because d(i + n/2) = -d(i). Since R = L + n/2, d(R) = -d(L). So we don't need to query a_R separately to know d(R) if we already know d(L). But wait, we need to be sure that d(L) is computed correctly. d(L) = a_L - a_{L+n/2}. We will query a_1 and a_{1+n/2} to get d(1). So d(R) = -d(1). So we have both signs without extra query. However, for the binary search to work, we need to evaluate d(mid) at each step. That requires two queries (unless we already have one of them). But we don't need to compute d(L) and d(R) at each step except for the initial interval. Actually we need to maintain that d(L) and d(R) have opposite signs. We know d(L) from initial? But as we update L and R, we need to know the sign of d at the new L or R. But when we set L=mid, we compute d(mid) at that iteration, so we know its sign. Similarly when we set R=mid, we know d(mid). So we always know the sign of both endpoints because we computed them when they were chosen as mid, or initially. So we only need to compute d(mid) when we examine mid. That's 2 queries each mid. And we need initial d(L) from 2 queries. So total queries = 2 + 2*#steps.
But we might also need d(R) initially to confirm opposite sign? We can deduce it from d(L), but not needed for algorithm; we just need to know that d(L) and d(R) are opposite, which we know because d(R) = -d(L) and d(L) != 0. But we need to ensure d(L) != 0; we can check that after initial queries; if zero, output immediately.
Thus algorithm steps for n/2 even:
- Query a1 and a_{1+n/2} (using two queries).
- Compute d1 = a1 - a_{1+n/2}.
- If d1 == 0: output ! 1.
- Else, set L = 1, R = 1 + n/2.
- While true:
- Let mid = (L + R) // 2 (integer division). If mid == L or mid == R, we might break? Actually we can continue while R - L > 1? We'll handle carefully.
- Query a_mid and a_opp where opp = mid + n/2 (if > n then subtract n). Compute d_mid = a_mid - a_opp.
- If d_mid == 0: output ! mid.
- Else, determine sign of d_mid relative to d(L) (or we can use the fact that d(L) and d(R) have opposite signs, and we know both signs. Actually we know sign(d_L) and sign(d_R) (d_R = -d_L). So if sign(d_mid) == sign(d_L), then set L = mid; else set R = mid.
- Continue.
We need to ensure that after updating, we maintain that d(L) and d(R) have opposite signs. As argued, if d_mid has same sign as d(L), then d_mid and d(R) have opposite signs (since d(R) opposite to d(L)). So new pair (mid, R) works. If d_mid has opposite sign to d(L), then it has same sign as d(R), so (L, mid) works. So fine.
Now, termination: When L and R become consecutive (R = L+1), we know that either d(L) = 0 or d(R) = 0 because they have opposite signs. But we haven't queried d(R) maybe? We might have d(L) known, but d(R) might not have been directly queried. However, we can compute d(R) by querying a_R and a_opp(R) if needed. But we could also break earlier: after each step, we compute d_mid; if we don't find zero, we update L or R. Eventually, the interval length becomes 1? Actually with integer indices, when L and R become adjacent, the next mid will be (L+R)//2, which equals L (if L+R odd? Let's see: if R = L+1, then (L+R)//2 = L (since integer division truncates). So mid = L. Then we would query d(L) again, which we already know. That would be wasteful, but we could break out. Instead, we can loop while R - L > 1. Inside loop, we compute mid = (L+R)//2; if mid equals L or R, break? Actually if R - L > 1, then mid is strictly between L and R. So we can continue. When R - L == 1, we stop. Then we know that d(L) and d(R) have opposite signs, so either L or R is zero. But we have already computed d(L) (since it was either initial or previously as mid). We may not have computed d(R). So we need to query d(R) if not known. Then if d(R)==0 output R else output L (since d(L) must be zero? Actually if both non-zero, it's impossible as argued; but just to be safe, we can check both. Since we are guaranteed existence, one will be zero.
But we could also avoid the extra query by ensuring we break when we find zero, and the loop condition ensures we always have mid distinct from L,R. At termination when R-L==1, we can simply output L if d(L)==0 else output R (if d(R)==0). But we need to know d(R). We may not have queried it yet. So we need to query a_R and a_opp(R) (2 queries) after loop. That's at most 2 extra queries, still within limit. Alternatively, we can incorporate that earlier: when we compute d_mid, if zero, we output. If not, we update. At the point when we exit because R-L==1, we can query both endpoints if needed. However, we might have already queried one of them. We can keep track of which indices we have queried. But it's simpler: after the while loop (R-L>1), we have L and R. We know d(L) (since it was either initial or computed when it became L). If we don't know d(R), query it. If d(R)==0, output R; else output L (since then d(L) must be 0). But we are guaranteed existence, so that will work.
But wait: Is it possible that we never computed d(L) after it became L? Actually L is set either to 1 initially (we computed d(1)) or to a mid whose d(mid) we computed. So we always know d(L). So we only need to possibly query d(R) if we haven't. R is set either to initial (1+n/2) or to a mid. If R is initial, we haven't directly queried d(R) because we deduced it as -d(1). But we may need its actual value to check zero? Actually if R is initial, we know d(R) = -d(1) which is non-zero (since d(1) non-zero). So we know it's not zero. So we don't need to query. But if R became mid at some step, we computed d(mid) at that time, so we know d(R) as well. So actually, both L and R are always indices for which we have computed d (either as initial d(1) or as some mid). Because whenever we set L=mid, we have d(mid). Similarly for R=mid, we have d(mid). The only potential index we might not have queried is the initial R = 1+n/2 if we never computed d for it. But we know its value from d(1) (since d(R) = -d(1)). However, is it possible that d(1) is zero? We already checked that case. So d(R) is non-zero and we know its sign. So we don't need to query it. So at the end when R-L==1, we know both d(L) and d(R) (since both have been computed or derived). Actually we need to be careful: if R is initial and we haven't explicitly computed a_R, we know d(R) = -d(1) but do we know the actual numeric value? We only know sign and that it's non-zero, but not the exact number? But that's enough to know it's not zero, and we know its sign. For the purpose of deciding which endpoint is zero, if one is zero we would know because we have its numeric value. If we haven't queried a_R, we wouldn't know if it's zero. But we argued that if both are non-zero and opposite signs, it's impossible for adjacent indices because step change would be at least 2 and cannot flip sign without zero. So one must be zero. Could it be that d(R) is zero? That would imply d(1)=0, which we already excluded. So d(R) cannot be zero. Therefore, d(L) must be zero. However, we have not verified that d(L) is zero; we just know d(L) maybe non-zero? Let's see: if d(L) is non-zero and d(R) is non-zero (and opposite signs), then as argued impossible for adjacent L,R. So at the point when R-L==1, it cannot be that both are non-zero and opposite signs; it must be that at least one is zero. But if d(R) is non-zero (and we know it's non-zero because d(R) = -d(1) ≠ 0), then d(L) must be zero. So we can output L without further queries. But we haven't actually computed d(L) value? We did compute d(L) when it became L. So we know whether it's zero or not. If it's non-zero, then the only possibility is that d(R) is zero, but we think d(R) is non-zero? Actually we might have set R to some mid that we computed and found non-zero. So both are non-zero, but then they cannot be adjacent with opposite signs. But wait, can we ever get to a situation where L and R are adjacent, both d(L) and d(R) non-zero, and have opposite signs? According to the step constraints, it's impossible. But can our binary search produce such a state? Let's simulate: start with L=1, R=1+n/2. Suppose d(1) >0, d(R) <0. They are not adjacent. We pick mid. Suppose d(mid) >0, so we set L=mid. Now L and R still have opposite signs. Continue. Could we end with L and R such that R = L+1 and both d(L) >0, d(R) <0? That would require d(L) >0, d(L+1) <0. Is that possible with the step constraints? For d(L+1) to be negative, the step from L to L+1 must be such that d(L+1) = d(L) + Δ, where Δ ∈ {-2,0,2}. So if d(L) = 2, then Δ could be -2 => d(L+1)=0, not negative. If d(L) = 4, Δ = -2 => 2 (positive), Δ = -4 not allowed. So the only way to get negative from positive is if d(L) = 2 and Δ = -4? not allowed. Or if d(L) = 0, then Δ = -2 gives -2 (negative). But d(L) positive. So indeed, if d(L) is positive, the smallest positive even is 2. Then the only possible changes are to 0,2,4. So cannot become negative in one step. Therefore, it's impossible for two consecutive indices to have opposite signs unless one is zero. So our binary search will never produce L,R adjacent with both non-zero opposite signs because if that happened, it would contradict the step property. However, could our algorithm erroneously set L and R such that they become adjacent without having checked the middle? But the step property is inherent to the sequence, not to our algorithm. So it's impossible for such a configuration to exist in the actual data. Therefore, when we get to R-L==1, we must have that either d(L)==0 or d(R)==0. Since we know d(L) and d(R) from previous computations, we can check which is zero (or if both? unlikely). If one is zero, we output that index.
But wait, could both be zero? Possibly if the sequence has multiple zeros. That's fine; we can output any. So we can check: if d(L)==0 output L; else output R.
Thus we might not need extra queries at the end.
But there is a nuance: what about the initial R? We never directly queried a_{R} but we know d(R) = -d(1). But if d(1) !=0, then d(R) !=0. So if later R becomes an endpoint after some updates, we might have set R to a mid that we queried, so we know its value. So by the time we narrow to adjacent indices, both L and R will have been explicitly queried because they were set as mid at some point. Because the only way an index becomes L or R is either initial L (queried) or a mid we queried. The initial R is never set as L? Actually we might keep R as initial for many steps if we always set L=mid. Eventually we might end with L some index and R still initial. In that case, R is the initial R, which we haven't queried directly. So if R = initial (1+n/2) and L is adjacent to R (i.e., L = R-1 or L = R+1?), since indices are in a line from 1 to 1+n/2, if L becomes R-1 (since L < R), then L = R-1. In that case, we have d(L) (known from when it became L), and R is initial. We have not directly queried d(R), but we know d(R) = -d(1) (non-zero). So we can infer that d(R) is non-zero. Then by the adjacent argument, d(L) must be zero. So we can output L without querying R. That's safe because we are certain that d(L) is zero? Wait, we haven't proven that d(L) is zero; we only know that if both d(L) and d(R) are non-zero, they cannot have opposite signs. But do we know they have opposite signs? Yes, because we maintain that d(L) and d(R) are opposite signs throughout. Since d(R) = -d(1) and d(1) has some sign, d(R) has opposite sign. And d(L) is same sign as d(1) (because we only moved L to indices where d has same sign as d(1)). Actually, careful: In the algorithm, we set L=mid when d(mid) has same sign as d(L) (which initially is d(1)). So all L indices we ever set will have the same sign as d(1). And R indices we set will have opposite sign (since we set R=mid when d(mid) has opposite sign to d(L), which means it has same sign as d(R)). So indeed, at any point, d(L) has the same sign as d(1) (positive if d(1)>0), and d(R) has opposite sign (negative). So they are opposite signs. So when R is initial, d(R) is opposite to d(1) (by construction), so opposite to d(L). So we have opposite signs. Now if L and R are adjacent, the only way for opposite signs to hold is if one of them is zero. Since d(R) is known to be non-zero (from d(1) ≠0), it must be that d(L) = 0. So we can confidently output L. But we haven't confirmed d(L) is zero by query? Actually we computed d(L) when L was set. So if d(L) were non-zero, then we would have a contradiction with the step property, meaning such a state cannot occur. But could it occur that d(L) is non-zero but d(R) is also non-zero and they are adjacent? Let's test with an example. Suppose n=8, and we have a sequence where d(1)=2, d(5)=-2, but are d(4) and d(5) adjacent? L=4, R=5, d(4)=? Could d(4) be 2 and d(5)=-2? That would be adjacent indices 4 and 5. But can such a sequence exist? Let's attempt to construct a valid a sequence for n=8 with steps such that d(4)=2, d(5)=-2. d(4) = a4 - a8, d(5)=a5 - a1. We need to check if constraints allow d(4)=2 and d(5)=-2. Let's attempt: Let a1=0 (wlog). Then a5? Actually we can try to assign steps. But we argued earlier that from d(4)=2 to d(5)=-2, the change is d(5)-d(4) = -4, but allowed change is d(5)-d(4) = d_4 - d_8? Actually formula: d(i+1)-d(i) = s_i - s_{i+n/2}. So from i=4 to 5, change = s_4 - s_8. Since s_4 and s_8 are ±1, the difference is in {-2,0,2}. So cannot be -4. So indeed impossible. So such a configuration cannot exist. Therefore, our binary search will never actually produce L and R adjacent with both non-zero opposite signs. In practice, before they become adjacent, we would have found a zero at some mid because the function must cross zero at some integer index. Actually, there might be a zero exactly at some index, and our binary search will eventually query that index when mid equals that index. So we will find zero before hitting the adjacency with both non-zero. So we can just loop until we find zero. The binary search will converge to an index where d=0. Because there is at least one zero. The standard binary search on a continuous function that changes sign will eventually narrow down to an interval where the zero is at an endpoint, but here because of discreteness, we may land exactly on zero. So we can keep doing mid calculations until we hit zero. With integer indices, the loop will terminate when we either hit zero or when L and R become consecutive? But if we never hit zero, then at the point when L and R are consecutive, the zero must be at one of them, but we would have already computed d for both? Actually if zero is at L or R, we might have not computed it because we only compute d(mid). If zero is at L, L was set to some index from a previous mid, but if that mid was exactly L and we computed d(L) at that time, we would have detected zero then. Similarly for R. So if zero is at an endpoint, we would have already detected it when that endpoint was assigned (as a mid). So it's likely we will find zero before the interval shrinks to adjacent. So we can just loop while we haven't found zero, and when L and R become adjacent, we can break and then check L and R (but we would have already checked them). Actually, in standard binary search for root finding, we stop when interval length is 1 and then check endpoints. But here we might not have checked the endpoints if they were never mid. However, if the zero is at an endpoint, that endpoint is either L=1 or R=1+n/2 initially, or some L or R that we set from a mid that we didn't compute because we set it as new bound without computing its d? Wait: When we set L=mid, we computed d(mid) at that iteration, so we know d(L) because L becomes mid. So if the zero is at that new L, we would have already output it. Similarly when we set R=mid. So the only endpoints that could be zero without us having computed them are the initial L=1 (we computed d(1)) and initial R (we didn't compute d(R) directly, but we can deduce it's not zero because d(1)≠0). So zero cannot be at initial R. So eventually we will either hit zero at some mid, or the interval will shrink until L and R are such that zero is at L or R, but those have been computed. So we can simply loop while true, compute mid, check zero, update bounds. When L+1 >= R? Actually we need to ensure we don't infinite loop. We can loop while True and break when we find zero. Since we are guaranteed existence, we will eventually get mid as zero. But could it happen that we keep narrowing and never hit zero because zero is exactly at an index that we never pick as mid? In binary search, if we always pick floor of mid, it's possible that we skip the zero if the interval length is 2 and zero is at the endpoint that is not mid? But we would have checked both endpoints? Let's examine.
Suppose we have interval [L,R] with L < R, and we know d(L) and d(R) opposite signs. If there is a zero at index Z. We pick mid = floor((L+R)/2). If Z == mid, we find it. If Z < mid, then d(L) and d(mid) have opposite signs? Not necessarily. But we use the rule: if sign(d(mid)) == sign(d(L)), then the zero is in (mid,R]; else in [L,mid). This rule is valid because d(L) and d(R) have opposite signs, and the function is such that there is at least one zero between them. The sign of d(mid) tells us which subinterval contains a zero. This works even if zero is at L or R? But L and R are non-zero. So if zero is exactly at mid, we stop. So standard binary search on a function with opposite signs at endpoints will converge to an interval where the zero is at the boundary. At the end, when L and R are consecutive, the zero must be either L or R. But in that case, our mid will be L (since (L+R)//2 = L). Then we compute d(L) again (already known). If d(L) is not zero, then we will check sign: if d(L) has same sign as d(L)? That's always true. So according to the rule, we would set L = mid? That would keep L unchanged, leading to infinite loop. So we need to handle the case when R = L+1.
Thus we need to explicitly check for zero when interval length becomes 2. So we can loop while R - L > 1. Inside, compute mid, etc. After loop, we have L and R with R = L+1. Then we know that d(L) and d(R) have opposite signs, so one is zero. Since we have d(L) from before (since L was either initial or a mid), we can check if d(L)==0 then output L else output R (and we may need to compute d(R) if not known). But as argued, if d(L) != 0, then d(R) must be zero. But we should compute d(R) to confirm, because we might not have its value. However, we are guaranteed existence, so it's safe, but to output the answer we need to output the index. So we need to output i such that a_i = a_{i+n/2}. So we must be certain which one is zero. The safe way: after loop, if d(L)==0, output L. else query d(R) (if not already known) and if zero output R; else (shouldn't happen) but we could fallback. Since we have query budget, we can afford one more query if needed.
But perhaps we can avoid extra query by noting that if d(L) != 0, then d(R) must be zero, and we can output R without verifying? But we need to be absolutely correct. In interactive problems, we must output correct answer. If our reasoning is rigorous, we can rely on it. The property that two consecutive indices cannot have opposite non-zero signs is a mathematical certainty given the constraints. So if d(L) ≠ 0, then d(R) = 0. So we can output R without querying. However, we must ensure that we indeed have d(L) computed correctly and it's non-zero. Since we computed d(L) from queries, it's accurate. So we can output R. But what if R is the initial R and we haven't queried a_R? We would be outputting R without having queried it. That's allowed? We just need to output an index i such that a_i = a_{i+n/2}. If our reasoning guarantees that R is such an index, it's fine. But we must be absolutely certain. Let's check the scenario: L and R adjacent, with R = initial R = 1+n/2, L = R-1 = n/2? Actually R = 1+n/2, so L = n/2. Then d(L) = a_{n/2} - a_{n} (since opposite of n/2 is n/2 + n/2 = n). We have computed d(L) when L became that index (maybe as a mid). d(L) is non-zero? Suppose d(L) is non-zero. Then d(R) = a_{1+n/2} - a_1 = -d(1). Since d(1) is non-zero, d(R) is non-zero. But we also have d(L) and d(R) opposite signs? Let's check signs: Since d(L) has same sign as d(1) (because L is set from mid that had same sign as d(1)), and d(R) has opposite sign. So indeed, d(L) and d(R) would have opposite signs and both non-zero, with indices L and R adjacent. Is that possible? We argued it's impossible due to step constraints. But let's test with a concrete example for n=8. Let L = 4, R = 5. We need d(4) >0, d(5) <0, and both non-zero. Is there a sequence satisfying that? Let's attempt to find one by brute reasoning. n=8, steps d_i (differences) from a_i to a_{i+1}. Let’s denote a1 arbitrary. We want d(4) = a4 - a8, d(5) = a5 - a1. Suppose d(4)=2, d(5)=-2. Then a4 = a8 + 2, a5 = a1 - 2. Also, a8 and a1 are related: a8 = a1 + sum_{i=1}^{7} d_i? Actually we have circle condition a1 = a1 + sum_{i=1}^{8} d_i => sum d_i =0. Also, a5 = a4 + d4? Actually a5 = a4 + d4. So a5 = a4 + d4. But d4 is the step from a4 to a5, which is ±1. So a5 - a4 = d4 ∈ {±1}. Compute a5 - a4 = (a1 - 2) - (a8 + 2) = a1 - a8 - 4 = -(a8 - a1) - 4 = -(a8 - a1) - 4. But a8 - a1 =? Since a8 = a1 + sum_{i=1}^{7} d_i, but we can also relate a8 to a1 via the opposite? Actually a8 is opposite of a4? Wait, opposite of 4 is 8? For n=8, opposite of 4 is 4+4=8, yes. So a8 = a4 + S4 where S4 = d4+d5+d6+d7? Actually a_{i+4} = a_i + sum_{k=0}^{3} d_{i+k}. So a8 = a4 + d4+d5+d6+d7. So a8 - a4 = d4+d5+d6+d7. But from d(4) = a4 - a8 => a8 = a4 - d(4) = a4 - 2. So a8 - a4 = -2. Thus d4+d5+d6+d7 = -2. Meanwhile, a5 = a4 + d4. So a5 - a4 = d4. Also from d(5) = a5 - a1 = -2 => a1 = a5 +2 = a4 + d4 +2. Then a8 =? Using a1 and circle: a8 = a7 + d7, but maybe we can compute consistency.
I suspect such a configuration leads to contradiction. Let's try to construct explicitly with numbers. Let's set a1=0 for simplicity. Then we need a5 = -2 (since d(5) = a5 - a1 = -2). So a5 = -2. Also d(4)=a4 - a8 =2 => a4 = a8 +2. Also, from a4 to a5 step d4: a5 = a4 + d4 => -2 = a4 + d4 => a4 = -2 - d4. Since d4 = ±1, a4 = -3 or -1. Meanwhile, a8 = a4 - 2 (from a4 = a8+2 => a8 = a4 -2). So a8 = -5 or -3. Now, consider the step from a8 to a1 (since circle): d8 = a1 - a8 = 0 - a8 = 5 or 3. But d8 must be ±1, contradiction. So indeed impossible. Thus our earlier reasoning stands: cannot have consecutive indices with opposite non-zero signs. So if we end up with L and R adjacent, and we know d(L) non-zero, then d(R) must be zero. So we can output R. However, in that scenario, we haven't queried a_R, but we can output R. That's acceptable because we are certain. However, to be extra safe (and within query limit), we could query d(R) and output if zero, else output L. That's fine. Since we have budget, we might just query both after loop to be safe. But we need to be careful: if R is initial, we have not queried a_R; querying it would cost 1 query (plus its opposite? Actually to compute d(R) we need a_R and a_{R+n/2} which is a_1 (already known). So just one query for a_R (since a_1 known). So that's 1 query, not 2. So at most 1 extra query. That's acceptable.
Thus we can implement:
if n/2 % 2 == 1:
print("! -1")
exit
else:
query a1 and a_{1+n/2}
d1 = a1 - a_{1+n/2}
if d1 == 0: print("! 1") exit
L = 1
R = 1 + n/2
# we know sign of d(L) = sign(d1)
# we know d(R) = -d1, non-zero
while R - L > 1:
mid = (L + R) // 2
# compute d(mid):
if mid <= n/2:
opp = mid + n/2
else:
opp = mid - n/2
query a_mid and a_opp # two queries
dmid = a_mid - a_opp
if dmid == 0:
print("! mid") exit
# decide which side
# we can compare sign with d(L) (since we know d(L) value)
if (dmid > 0 and d1 > 0) or (dmid < 0 and d1 < 0):
# same sign as left
L = mid
else:
R = mid
# Now R = L+1
# we have d(L) known (since L is either 1 or a mid)
# we may need to check if d(L) == 0 (could be if we missed)
if d(L) == 0: # but we would have output earlier if zero
print("! L")
else:
# then d(R) must be zero. But to be safe, we can query a_R and a_opp(R) if needed.
# compute oppR
if R <= n/2:
oppR = R + n/2
else:
oppR = R - n/2
# we might already have a_oppR? oppR is L? Not necessarily. But we can query.
# But we can also rely on reasoning and output R without query. But let's query to be safe.
# However, if we query, we need a_R and a_oppR. We may already have a_oppR if it's a1 or something.
# To simplify, we can query a_R (if not already) and compute dR = a_R - a_oppR.
# But we might have already queried a_oppR earlier? Possibly not.
# Since we have budget, we can query both a_R and a_oppR (2 queries). But we can also note that oppR is either L+n/2? Not sure.
# But we can just query d(R) as we would for any index: two queries.
# However, if d(R) turns out not zero, then something is wrong, but we can fallback to L.
# But to minimize risk, we can compute d(R) and output accordingly.
query a_R and a_oppR
dR = a_R - a_oppR
if dR == 0:
print("! R")
else:
# then d(L) must be zero; but we already know d(L) !=0? Actually if dR not zero, then both non-zero, contradiction; but that can't happen. So we can output L as fallback.
print("! L")
exit
But note: The while loop might exit with R - L == 1 and we have not yet checked d(L) for zero? Actually if d(L) were zero, we would have found it earlier because when L was set (as mid), we computed d(L) and would have output. However, it's possible that L was initially 1 and we checked d1 !=0, so L is not zero. So d(L) is non-zero. So the else case will trigger and we query d(R). d(R) should be zero, so we output R. So we don't need fallback.
But we must ensure we don't exceed 60 queries. Let's compute worst-case number.
n up to 100k, n/2 up to 50k. log2(50k) ~ 16. So number of mid queries ≤ 16. Each mid query: 2 queries. So ≤ 32. Plus initial 2 queries = 34. Plus possibly one extra query for d(R) at end (2 queries? Actually if we query d(R) we need two numbers, but we might already know one (e.g., a_oppR might be known). In worst case, we might need 2 more queries. So total ≤ 36. So well within 60.
But we can also reduce initial queries to 1 if we combine? Actually we need a1 and a_{1+n/2} to compute d1. That's 2. Could we start with L=1, R=1+n/2, and compute d(L) by querying a1 and its opposite? That's 2. So fine.
Now, one more nuance: The parity check: if n/2 is odd, we output -1 without queries. But is it always valid? Yes, because f(i) odd for all i. However, we must be absolutely sure that the condition "for every two adjacent students, these numbers differ exactly by one" is always satisfied. That's given. So we are safe.
But wait, what about n=2? n/2=1 odd, so we output -1 without queries. Indeed, with two students adjacent and opposite, they must differ by 1, so cannot be equal. So correct.
Now, need to ensure we handle 1-indexing properly.
Edge Cases:
-
n=2, n/2 odd -> output -1.
-
n=4, n/2=2 even -> we must find a zero. Let's test: Suppose a = [1,2,1,0]? Actually need adjacent diff 1: 1->2 diff1, 2->1 diff1, 1->0 diff1, 0->1 diff1. So a1=1,a3=1 -> zero at i=1. Our algorithm: query a1 and a3 (since n/2=2). d1 = 0? If it's zero, output 1. So found.
-
n=4 with a = [0,1,2,1]? Then a1=0,a3=2 => d1=-2 (even non-zero). Then L=1, R=3. mid=2. Query a2 and a4: a2=1, a4=1 => d2=0 -> output 2. Works.
-
n=8, sample: a: 1,2,1,2,3,4,3,2. Let's test algorithm: n/2=4 even. query a1=1, a5=3 => d1=-2. L=1,R=5. mid=3. Query a3 and a7: a3=1, a7=3 => d3=-2, same sign as d1 (negative). So set L=3. Now interval [3,5]. mid=4. Query a4 and a8: a4=2, a8=2 => d4=0 -> output 4. Works.
Thus the algorithm is correct.
Now, implementation details:
We need to write an interactive program that reads n, then performs queries.
We must flush after each output.
We'll implement a function to query an index i, returns a_i. We'll use input() to read the response. But careful: The input format: Initially we are given n via input. Then after each "? i" we read the number a_i. There's no extra lines? The examples show that after each query, the response is on a separate line. So we can print "? i", flush, then read int from input.
We must handle the case where we output answer with "! i" and exit.
Now, ensure that when we compute opposite index, we use 1-indexing.
opp = i + n//2
if opp > n: opp -= n
Because n//2 is integer division (since n even). Actually n//2 = n/2.
Now, for initial R = 1 + n//2, opp of R is (1 + n/2 + n/2) mod n = 1 (since n+1 mod n =1). So consistent.
Now, the while loop condition: while R - L > 1:
Inside, compute mid = (L+R)//2 (integer). Since L and R are both in 1..n, and L < R, mid will be between them.
We need to be careful about the case when mid equals L? With R-L>1, mid will be at least L+1, so fine.
Now, after loop, we have R = L+1.
Now, we already have d(L) value? We stored it when we last computed d for index L. But we need to keep a dictionary or variable for d(L). Since we update L or R, we should store the d value for the current L and R.
We can store variables dL and dR representing the known d values for L and R. Initially, after querying a1 and opp(1), we have d1 = a1 - a_{1+n/2}. Set dL = d1. For R, we know dR = -d1, but we don't have its actual a_R? But we know its sign and that it's non-zero. However, for the algorithm we only need to compare signs, so we can store signR = -sign(dL). But when we later set R=mid, we compute d(mid) and set dR = that value. So we can keep dL and dR as integers (with dR initially set to -dL, but note that it's not queried, but we can treat it as -dL, which is accurate. However, careful: dR = a_R - a_opp(R). For R = 1+n/2, opp(R) = 1, so dR = a_R - a_1. Since we have a_1, but not a_R, we cannot compute exact numeric value unless we query. But we can deduce dR = -dL because dL = a1 - a_{1+n/2} = a1 - a_R, so a_R - a1 = -dL. So indeed dR = -dL exactly. So we can store dR = -dL. So we have numeric values for both ends. That's fine because we can compute them exactly without extra query: dR = -dL. But we need to be careful: This holds only while R remains the initial R. Once we update R to some mid, we compute its d and set dR accordingly. Similarly for L.
So we can maintain dL and dR as actual values.
Now, when we compute dmid, we compare its sign with dL (or we could compare with dR). Since dL and dR have opposite signs, comparing with dL is enough.
Now, after the loop, we need to output answer. We can first check if dL == 0 (should not happen because we would have output earlier), but if it is, output L. Else, we know dR must be zero? But we might not have the actual dR if R is still initial? Actually if R is still initial, we have dR = -dL, which is non-zero. So dR is not zero. That would contradict our earlier deduction that at adjacent indices, one must be zero. But R being initial and L adjacent to it, we argued d(L) non-zero => d(R) must be zero, but our stored dR = -dL non-zero. This indicates such a situation cannot occur because the data would be inconsistent, so our algorithm would never produce L and R adjacent with R initial and L adjacent while dL non-zero? Let's test if it's possible to have L = n/2, R = n/2+1 (adjacent) with d(L) non-zero and d(R) non-zero? We already argued it's impossible. Therefore, this situation will not arise. In practice, we will have updated R at some point, so R will not remain initial when interval shrinks to size 2. Because to get to size 2, we must have set either L or R to mid at some point. If we always set L=mid when dmid has same sign as dL, then R remains initial until we get a dmid with opposite sign, at which point we set R=mid. So eventually R will become a mid. So by the time the interval length becomes 2, both L and R will have been explicitly set (i.e., not the original R). But it's possible that we never set R, i.e., we always set L=mid, and eventually L becomes something and R remains initial, and the interval length becomes 2? Let's simulate: start L=1, R=1+n/2. Suppose n/2=4, so R=5. Length 4. mid=3. Suppose d(3) has same sign as d(1), so set L=3. Now interval [3,5], length 2. Wait, R-L = 5-3=2, not 1. So length is 2? Actually R-L = 2. So while condition R-L > 1 would still be true? Since 2 > 1, we go again. Now mid = (3+5)//2 = 4. Compute d(4). If d(4) has same sign as d(L) (d(3) same as d(1)), set L=4 -> interval [4,5] length 1? Actually 5-4=1, loop condition false. So after setting L=4, we exit loop with L=4, R=5. In this scenario, we have L=4 (mid we just computed), R=5 (initial). So R is still initial. And we have d(L) known (computed as d(4)). d(L) could be non-zero (if d(4) not zero). Now, we have R = 5, and we haven't computed d(5) directly, but we have dR = -d1? Actually dR is still -d1, but that might not equal the true d(5) if d(5) is something else? Wait, is dR = -d1 still valid? dR was defined as the d value at index R. Since we never updated R, we are still using the initial R=5. But does the value d(5) equal -d(1) always? Yes, by property d(i+n/2) = -d(i). So d(5) = -d(1). That is a fixed mathematical truth, independent of our queries. So d(5) is indeed -d(1). So we can safely say dR = -dL_initial? But careful: dL_initial is d(1). So d(5) = -d(1). So we have d(5) known exactly without query. So we have dR numeric value = -d(1). Now, d(L) = d(4). In this scenario, we have L=4, R=5. Are they adjacent? Yes. We have d(4) (computed) and d(5) = -d(1). Could both be non-zero and opposite signs? Possibly. For n=8, d(4) could be 2, d(5) = -2? But we earlier argued that's impossible. So this situation cannot actually occur if the data respects the constraints. But our algorithm, without knowing the impossibility, might produce it? But it cannot produce it because the actual data won't allow it. Therefore, in any valid input, when we get to L=4,R=5, we would have found d(4)=0 or maybe d(4) such that d(5) not opposite? But we know d(5) = -d(1). If d(4) is non-zero and same sign as d(1), then d(4) and d(5) have opposite signs. Then they would be consecutive with opposite non-zero signs, which is impossible. So such a state cannot occur. Therefore, either d(4) will be zero (so we would have output earlier) or d(4) will have opposite sign to d(1) (so we would have set R=4 earlier, not L). Let's see: In the iteration with mid=4, we compute d(4). If d(4) has same sign as d(L) (which is d(3) same as d(1)), then we would set L=4. That yields the problematic state. But if that were to happen, the data would be inconsistent. So it cannot happen. Thus, in practice, when we compute d(4), it will either be zero or have opposite sign to d(L). So we will set R=4, not L=4. Then interval becomes [3,4] length 1? Actually L=3,R=4 -> length 1, loop exit. Then L=3,R=4, both indices have d known? L=3 we computed d(3), R=4 we computed d(4). So we have both. And if neither is zero, they must have opposite signs. But as argued, two consecutive indices cannot have opposite non-zero signs. So one must be zero. So after loop, we can check: if d(L)==0 output L else output R. So it's safe. So the scenario where R remains initial at loop exit is impossible because we would have set R earlier. Let's double-check with n=8: start L=1,R=5 (d1 pos? maybe). mid=3: if d3 same sign, set L=3. Now interval [3,5]. mid=4. d4 computed. If d4 same sign as d(L) (which is same as d1), then we set L=4 -> interval [4,5]. But as argued, that cannot happen because d4 same sign as d1 and d5 opposite => consecutive with opposite signs and non-zero impossible. So d4 cannot have same sign as d1 if it's non-zero? Could it be zero? Then we would have output. So indeed, d4 must have opposite sign, so we set R=4, giving interval [3,4] and then loop exit. So R gets updated. So at exit, both L and R are mid indices we have computed. So we have both d values. So we can simply output whichever is zero (or if both non-zero? can't happen). So we don't need to query extra.
Thus, we can implement after loop:
if d(L) == 0:
ans = L
else:
ans = R # because d(R) must be zero
But we must be careful that we have d(R) stored. In the case where R was updated, we have its d. In the case where R was never updated (impossible), we wouldn't have d(R) stored as numeric? But we argued it's impossible, so not needed.
Therefore, final steps: after loop, if d(L) == 0 print L else print R.
Now, we need to maintain dL and dR as actual values. Initialize:
query a1 and a_{1+n/2}
a1 = val1, a1pn = val2
d1 = a1 - a2? Actually a_{1+n/2} we call a_mid. So d1 = a1 - a_{1+n/2}
Set L=1, dL = d1.
Set R = 1 + n//2, dR = -d1 (since we know that mathematically)
Now, while R - L > 1:
mid = (L+R)//2
# query a_mid and a_opp(mid)
a_mid = query(mid)
opp = mid + n//2; if opp > n: opp -= n
a_opp = query(opp)
dmid = a_mid - a_opp
if dmid == 0:
print("! " + str(mid)); return
# determine side
# we need to compare sign of dmid with dL (or we can use product with dL)
if (dL > 0 and dmid > 0) or (dL < 0 and dmid < 0):
# same sign as left, so zero is to the right
L = mid
dL = dmid
# dR remains unchanged
else:
# opposite sign => zero is to the left
R = mid
dR = dmid
# dL unchanged
After loop:
if dL == 0:
ans = L
else:
ans = R
print("! " + str(ans))
Now, we need to ensure that when we set L=mid, we are confident that the interval [L,R] still contains a zero. Since dL and dR have opposite signs, yes.
One more nuance: When we compute dmid, we used two queries. Could we potentially reuse one of the values if we have already queried that index? Possibly but not necessary.
Now, check if we ever exceed 60 queries. Worst-case number of loop iterations: The interval size starts at n/2. Each iteration reduces the size roughly by half. The number of iterations until size becomes 1 is ceil(log2(n/2)). For n=100k, n/2=50k, log2(50000) ≈ 15.6, so up to 16 iterations. So total queries = 2 (initial) + 2*16 = 34. Plus maybe final check? No extra. So ≤ 34.
Now, confirm with n=100000, n/2=50000, log2(50000)=15.6 -> 16 iterations. Good.
Now, parity check: n/2 odd -> output -1 without queries. But we must be careful: The problem states "You can ask questions... and the goal is to determine whether the desired pair exists in no more than 60 questions." It doesn't forbid using zero questions. So outputting without any query is allowed. However, we need to read n first. That's not counted as query. So we can directly output "! -1". That's acceptable.
But wait, the problem statement: "The participants are numbered so that participant i and participant i+1 (1 ≤ i ≤ n-1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one." So the condition is strict. So parity reasoning stands.
Thus solution is simple.
But before finalizing, let's double-check parity reasoning for n/2 odd: sum of n/2 steps each ±1, sum is odd because odd number of odd terms. So a_i - a_{i+n/2} = -sum, which is odd, cannot be 0. So indeed impossible.
Thus we can directly answer -1.
But is there any possibility that the numbers are not integers? They are integers, given bounds -10^9 to 10^9.
All good.
Now, consider the possibility of n=0? No, n≥2 even.
Now, we should also consider that the problem might be from Codeforces and some official solutions use similar reasoning.
Now, we must implement interactive I/O properly. We'll use sys.stdout.write and sys.stdin.readline, or print with flush. We'll define a function ask(i) that prints "? i", flushes, reads int.
Implementation in Python:
import sys
def query(i):
print("?", i)
sys.stdout.flush()
return int(sys.stdin.readline())
Then main:
def main():
import sys
n = int(sys.stdin.readline())
half = n // 2
if half % 2 == 1:
print("! -1")
sys.stdout.flush()
return
# else
a1 = query(1)
opp1 = 1 + half
a_opp1 = query(opp1)
d1 = a1 - a_opp1
if d1 == 0:
print("!", 1)
sys.stdout.flush()
return
L = 1
dL = d1
R = 1 + half
dR = -d1 # because d(R) = -d1
while R - L > 1:
mid = (L + R) // 2
a_mid = query(mid)
opp = mid + half
if opp > n:
opp -= n
a_opp = query(opp)
dmid = a_mid - a_opp
if dmid == 0:
print("!", mid)
sys.stdout.flush()
return
# compare signs
if (dL > 0 and dmid > 0) or (dL < 0 and dmid < 0):
# same sign as left
L = mid
dL = dmid
# dR unchanged
else:
R = mid
dR = dmid
# dL unchanged
# now R = L+1
if dL == 0:
ans = L
else:
ans = R
print("!", ans)
sys.stdout.flush()
if name == "main":
main()
But we need to ensure that when we compute dR = -d1 initially, we treat it as numeric. That's fine.
One potential bug: In the while loop, we use dL for sign comparison. But what if dL is zero? It cannot be because we checked d1 !=0 and we only update L to mid if dmid has same sign as dL, which is non-zero. So dL stays non-zero. Similarly dR stays non-zero? Could dR become zero? If we set R to mid and dmid==0, we would have returned already. So dR non-zero.
Now, sign comparison: Use (dL * dmid > 0) might be simpler, but careful about overflow? Not an issue in Python. But we must consider that dL and dmid are integers. So if both positive or both negative, product positive. So condition: if dL * dmid > 0: same sign. This also works if one is zero? But we already exclude zero. So we can use that.
Thus we can write: if dL * dmid > 0: L = mid; dL = dmid else: R = mid; dR = dmid.
Now, one more nuance: When we set R=mid, we are updating dR. But note that after updating, the interval [L,R] still has dL and dR opposite signs? Yes because originally dL and dR (old) opposite. If dmid has opposite sign to dL, then dmid has same sign as old dR, so new dR (which is dmid) has same sign as old dR, and dL unchanged, so they remain opposite. So property holds.
Now, final output: ans = L if dL == 0 else R. But we haven't explicitly checked dR zero? Could dR be zero? If dR is zero, we would have output earlier because we only set R to mid if dmid !=0. But could dR be zero initially? That would mean d1=0, already output. So fine.
Thus answer is correct.
Now, test with sample 1: n=8 half=4 even. a1=1, a5=3 => d1=-2. L=1,dL=-2; R=5,dR=2. Loop: R-L=4>1, mid=3. query a3=1, opp=7? 3+4=7, a7=3 => d3=-2. dL*d3 = 4>0 => same sign, set L=3,dL=-2. Now interval [3,5], R-L=2>1, mid=4. query a4=2, opp=8? 4+4=8, a8=2 => d4=0 -> output !4. Works.
Sample 2: n=6 half=3 odd -> output ! -1 directly, which matches the answer (they output -1 after queries, but we output immediately). That's acceptable.
Now, need to be careful with input: The first line contains n. There might be extra spaces. We'll use int(input()).
Edge case: n=2, half=1 odd -> output -1.
Now, ensure flush after each output.
Now, is there any scenario where n/2 even but we might incorrectly output -1? No, we always find a zero.
Now, test with some custom cases.
Case n=4, half=2 even. Let's generate random valid sequence? We can brute. For n=4, all valid sequences have sum of steps zero. Example: a = [0,1,0,-1]? Check adjacency: 0->1 diff1, 1->0 diff1, 0->-1 diff1, -1->0 diff1 (since -1 to 0 diff1). Opposite pairs: 1 vs 3:0 vs 0 equal, so zero exists. Our algorithm: query a1 and a3 => d1=0 -> output 1. Good.
Another: a = [0,1,2,1] -> a1=0,a3=2 => d1=-2. L=1,R=3. mid=2. query a2=1, opp=4? 2+2=4 => a4=1 => d2=0 -> output 2.
All good.
Now, for n=8, let's try a case where the zero is at index 2 maybe. Build sequence: Let's define steps d1..d8 such that sum=0. Want a1 - a5 = non-zero, and a2 - a6 =0. Let's attempt: a1=0. Choose steps: d1=1, d2=1, d3=-1, d4=-1, d5=1, d6=-1, d7=1, d8=-1? Sum=1+1-1-1+1-1+1-1=0. Then compute a: a1=0, a2=1, a3=2, a4=1, a5=0, a6=1, a7=0, a8=1. Check adjacency: a8 to a1:1-0=1 ok. Opposite: a2=1, a6=1 => zero at i=2. Our algorithm: query a1=0, a5=0 => d1=0? Actually a5=0, so d1=0 -> output 1. That's also a zero (i=1). Actually a1=0, a5=0, so indeed zero at i=1 as well. So fine.
Now, need to ensure that when d1 !=0, we proceed. Example where only zero is at i=2: Let's try to avoid zero at i=1. Need a1 != a5. Let's search: a1=0, want a5 !=0. Choose steps: d1=1, d2=1, d3=1, d4=-1, d5=-1, d6=-1, d7=1, d8=-1? Sum=1+1+1-1-1-1+1-1=0? Compute: 1+1=2, +1=3, -1=2, -1=1, -1=0, +1=1, -1=0. So sum zero. a1=0, a2=1, a3=2, a4=3, a5=2, a6=1, a7=0, a8=1. Opposite: a1=0 vs a5=2 (diff -2), a2=1 vs a6=1 (0), a3=2 vs a7=0 (2), a4=3 vs a8=1 (2). So zero at i=2 only. d1=-2. L=1,R=5. mid=3: query a3=2, opp=7? 3+4=7 => a7=0 => d3=2. dL=-2, d3=2, product -4 <0 => opposite sign, so set R=3. Now interval [1,3], R-L=2>1, mid=2. query a2=1, opp=6 => a6=1 => d2=0 -> output 2. Works.
Now, test a case where zero is at i=4 (like sample). Already did.
Now, test n=10? n/2=5 odd -> output -1. But we must be sure that no zero can exist. Indeed f(i) odd, so can't be zero. So correct.
Now, we need to confirm that we never exceed query limit for n up to 100k with n/2 even. n/2 up to 50k, log2(50000)=~16. So at most 2+2*16=34 queries. Good.
Now, one more nuance: The problem statement says "You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions." Our solution uses at most 34 queries in worst-case for n/2 even, and 0 for n/2 odd. So it's within limit.
Now, we should also handle the case when n is large and we might need to worry about integer overflow? Not in Python.
Now, implement.
Potential pitfalls: Off-by-one in opposite calculation. We'll compute opp = i + half; if opp > n: opp -= n. Since half = n//2, i+half can be at most n + (n/2) but i max is n, so i+half can be up to n + n/2; subtract n if > n. That gives correct opposite because for i > n/2, opp = i - n/2.
Let's test: n=8, half=4. i=5: opp=5+4=9>8 => opp=9-8=1, correct opposite. i=8: opp=8+4=12>8 => opp=12-8=4, opposite of 8 is 4? Since 8+4=12 mod8=4, yes.
Now, for i=4: opp=4+4=8, correct.
So good.
Now, initial opposite for index 1: opp=1+half = 1+half <= n because half <= n-1? For n>=2, 1+half <= n? For n=2, half=1, 1+1=2 == n. For n=4, half=2, 1+2=3 <=4. So safe.
Now, we also need to handle reading after queries: The response is an integer, possibly negative. So int(input()) works.
Now, about flushing: In Python, we can use sys.stdout.flush() after each print. Or we can set sys.stdout = ... but simpler: print("?", i, flush=True) in Python 3. Using flush=True is easier. So we can do: print(f"? {i}", flush=True).
Now, implement with flush=True.
Now, final code.
But we must also consider that the input might have extra whitespace lines. The interactive runner will output the response immediately after our query. So reading with input() should work.
Now, we'll write the final solution.
One more test: n=2, half=1 odd -> output "! -1". That's correct.
Now, I think we are done.
But wait, check the problem statement: "The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one." There's no further constraint. So our reasoning holds.
Thus final answer.
Now, I'll provide final solution code in Python.
But before finalizing, recall that the problem might be known as "The Hat" and there is an editorial that explains exactly this solution. So it's correct.
Now, I'll also add a comment about parity decision.
Now, output the solution. ```python
import sys
def solve() -> None:
n = int(sys.stdin.readline())
half = n // 2
# if half is odd, f(i) is always odd → can never be zero
if half % 2 == 1:
print("! -1")
sys.stdout.flush()
return
# half is even → a solution always exists
# query a1 and a_{1+half}
a1 = ask(1)
a1h = ask(1 + half)
d1 = a1 - a1h
if d1 == 0:
answer(1)
return
L, dL = 1, d1
R, dR = 1 + half, -d1 # because d(i+half) = -d(i)
while R - L > 1:
mid = (L + R) // 2
a_mid = ask(mid)
opp = mid + half
if opp > n:
opp -= n
a_opp = ask(opp)
dmid = a_mid - a_opp
if dmid == 0:
answer(mid)
return
# which sub-interval contains the zero?
if dL * dmid > 0: # same sign as left
L, dL = mid, dmid
else:
R, dR = mid, dmid
# now R = L+1, and one of them is zero
ans = L if dL == 0 else R
answer(ans)
def ask(i: int) -> int:
print(f"? {i}", flush=True)
return int(sys.stdin.readline())
def answer(i: int) -> None:
print(f"! {i}", flush=True)
sys.exit(0)
if name == "main":
solve()