Codeforces Round 1073 (Div. 1)
F
Xor Product
Codeforces
3300
原题
← EG →
翻译中…

For non-negative integers $x, y$ and a positive integer $k$, let $S(x, y, k)$ be the set of values $(x + i) \oplus (y + j)$ for all $0 \le i, j \lt k$. Formally: $$ S(x, y, k) = \{ (x + i) \oplus (y + j) \mid 0 \le i, j \lt k \} $$ where $\oplus$ denotes the bitwise XOR operation.

翻译中…

Define $f(x, k)$ as the maximum size of $S(x, y, k)$ over all non-negative integers $y$ (that is, $y \ge 0$).

翻译中…

You are given integers $x$ and $k$. Compute $f(x, k)$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

Each of the next $t$ lines contains two integers $x$ and $k$ ($1 \le x, k \le 10^{17}$).

翻译中…

Output

翻译中…

For each test case, output a single integer — the value of $f(x, k)$.

翻译中…

Note

翻译中…

In the first example, since $k=1$, the set $S$ will always contain exactly one element regardless of $y$. For instance, if we pick $y = 69$, we have $S(67, 69, 1) = \{67 \oplus 69\} = \{6\}$, so $|S(x, y, k)| = 1$.

翻译中…

In the second example, we have $x = 7$ and $k = 3$. The optimal choice is $y = 8$. The values of $(x + i) \oplus (y + j)$ are: $$ [7 \oplus 8, 7 \oplus 9, 7 \oplus 10, 8 \oplus 8, 8 \oplus 9, 8 \oplus 10, 9 \oplus 8, 9 \oplus 9, 9 \oplus 10] $$ which simplifies to $[15, 14, 13, 0, 1, 2, 1, 0, 3]$. The set of distinct values is $S(7, 8, 3) = \{0, 1, 2, 3, 13, 14, 15\}$, so the size is $7$. It can be shown that no other $y$ yields a larger size. However, the choice of $y$ matters; for example, if you chose $y = 22$, you would get $S(7, 22, 3) = \{16, 17, 30, 31\}$ with size $4$, which is suboptimal.

翻译中…

In the sixth example, after countless calculations, we managed to figure out that the optimal $y$ is $278\,302\,368\,699\,121\,665$, which gives an answer of $398\,158\,383\,604\,301\,822$. The proof is left to the reader as a trivial exercise.

翻译中…

样例

Input #1
6
67 1
7 3
100 12
1 1043
1526 1043
88946092640567295 100000000000000000
Output #1
1
7
32
3128
4167
398158383604301822
InputCopy OutputCopy
题目信息
题号
2190F
时间限制
2 s
内存限制
256 MB
标签
位运算
动态规划

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。