CF BUDDY
← Problems·

1936A · Bitwise Operation Wizard

1700 · bitmasks, constructive algorithms, greedy

Problem: This is an interactive problem.

There is a secret sequence p0,p1,,pn1p_0, p_1, \ldots, p_{n-1}, which is a permutation of {0,1,,n1}\{0,1,\ldots,n-1\}.

You need to find any two indices ii and jj such that pipjp_i \oplus p_j is maximized, where \oplus denotes the bitwise XOR operation.

To do this, you can ask queries. Each query has the following form: you pick arbitrary indices aa, bb, cc, and dd (0a,b,c,d<n0 \le a,b,c,d < n). Next, the jury calculates x=(papb)x = (p_a \mid p_b) and y=(pcpd)y = (p_c \mid p_d), where | denotes the bitwise OR operation. Finally, you receive the result of comparison between xx and yy. In other words, you are told if x<yx < y, x>yx > y, or x=yx = y.

Please find any two indices ii and jj (0i,j<n0 \le i,j < n) such that pipjp_i \oplus p_j is maximum among all such pairs, using at most 3n3n queries. If there are multiple pairs of indices satisfying the condition, you may output any one of them.

Input Format: Each test contains multiple test cases. The first line contains the number of test cases tt (1t1031 \le t \le 10^3). The description of the test cases follows.

Note: In the first test case, the hidden permutation is p=[0,3,1,2]p=[0,3,1,2].

For the query "? 0 2 3 1", the jury return "<" because (p0p2)=(01)=1<(p3p1)=(23)=3(p_0 \mid p_2) = (0 \mid 1) =1 < (p_3 \mid p_1) = (2 \mid 3) = 3.

For the query "? 1 1 2 3", the jury return "=" because (p1p1)=(33)=3=(p2p3)=(12)=3(p_1 \mid p_1) = (3\mid 3)= 3 = (p_2 \mid p_3) = (1 \mid 2)=3.

For the query "? 1 2 0 3", the jury return ">" because (p1p2)=(31)=3>(p0p3)=(02)=2(p_1 \mid p_2) = (3 \mid 1) = 3 > (p_0 \mid p_3) = (0\mid 2)=2.

The answer i=3i = 3 and j=2j = 2 is valid: (p3p2)=(21)=3(p_3 \oplus p_2) = (2 \oplus 1) = 3 is indeed equal to the maximum possible value of pipjp_i \oplus p_j. Another valid answer would be i=0i=0 and j=1j=1. As the number of queries does not exceed 3n=123n=12, the answer is considered correct.

In the second test case, n=2n = 2, so pp is either [0,1][0, 1] or [1,0][1, 0]. In any case, p0p1=1p_0 \oplus p_1 = 1 is maximum possible.

Sample Cases

Case 1

Input

2
4

<

=

>

2

Output

? 0 2 3 1

? 1 1 2 3

? 1 2 0 3

! 3 2

! 0 1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback