CF BUDDY
← Problems·

1979B · XOR Sequences

1000 · bitmasks, greedy

Problem: You are given two distinct non-negative integers xx and yy. Consider two infinite sequences a1,a2,a3,a_1, a_2, a_3, \ldots and b1,b2,b3,b_1, b_2, b_3, \ldots, where

  • an=nxa_n = n \oplus x;
  • bn=nyb_n = n \oplus y.

Here, xyx \oplus y denotes the bitwise XOR operation of integers xx and yy.

For example, with x=6x = 6, the first 88 elements of sequence aa will look as follows: [7,4,5,2,3,0,1,14,][7, 4, 5, 2, 3, 0, 1, 14, \ldots]. Note that the indices of elements start with 11.

Your task is to find the length of the longest common subsegment^\dagger of sequences aa and bb. In other words, find the maximum integer mm such that ai=bj,ai+1=bj+1,,ai+m1=bj+m1a_i = b_j, a_{i + 1} = b_{j + 1}, \ldots, a_{i + m - 1} = b_{j + m - 1} for some i,j1i, j \ge 1.

^\daggerA subsegment of sequence pp is a sequence pl,pl+1,,prp_l,p_{l+1},\ldots,p_r, where 1lr1 \le l \le r.

Input Format: Each test consists of multiple test cases. The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases. The description of the test cases follows.

The only line of each test case contains two integers xx and yy (0x,y109,xy0 \le x, y \le 10^9, x \neq y) — the parameters of the sequences.

Output Format: For each test case, output a single integer — the length of the longest common subsegment.

Note: In the first test case, the first 77 elements of sequences aa and bb are as follows:

a=[1,2,3,4,5,6,7,]a = [1, 2, 3, 4, 5, 6, 7,\ldots]

b=[0,3,2,5,4,7,6,]b = [0, 3, 2, 5, 4, 7, 6,\ldots]

It can be shown that there isn't a positive integer kk such that the sequence [k,k+1][k, k + 1] occurs in bb as a subsegment. So the answer is 11.

In the third test case, the first 2020 elements of sequences aa and bb are as follows:

a=[56,59,58,61,60,63,62,49,48,51,50,53,52,55,54,41, 40, 43, 42,45,]a = [56, 59, 58, 61, 60, 63, 62, 49, 48, 51, 50, 53, 52, 55, 54, \textbf{41, 40, 43, 42}, 45, \ldots]

b=[36,39,38,33,32,35,34,45,44,47,46,41, 40, 43, 42,53,52,55,54,49,]b = [36, 39, 38, 33, 32, 35, 34, 45, 44, 47, 46, \textbf{41, 40, 43, 42}, 53, 52, 55, 54, 49, \ldots]

It can be shown that one of the longest common subsegments is the subsegment [41,40,43,42][41, 40, 43, 42] with a length of 44.

Sample Cases

Case 1

Input

4
0 1
12 4
57 37
316560849 14570961

Output

1
8
4
33554432

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