AtCoder Regular Contest 191 (Div. 2)
B
XOR = MOD
AtCoder
⌀ 1111
原题
← AC →
翻译中…

Problem Statement

翻译中…

You are given two positive integers $N$ and $K$. A positive integer $X$ is called compatible with $N$ if it satisfies the following condition:

翻译中…
  • The bitwise XOR of $X$ and $N$ is equal to the remainder when $X$ is divided by $N$.
翻译中…

Determine whether there exist at least $K$ such integers $X$ that are compatible with $N$. If they do exist, find the $K$-th smallest such integer.

翻译中…

You are given $T$ test cases; solve each of them.

翻译中…

About XOR The bitwise XOR of nonnegative integers $A$ and $B$, denoted $A\ \mathrm{XOR}\ B$, is defined as follows: In the binary representation of $A\ \mathrm{XOR}\ B$, the digit in the $2^k$ place (for $k \geq 0$) is $1$ if and only if exactly one of $A$ and $B$ has a $1$ in the $2^k$ place in its binary representation. Otherwise, it is $0$. For example, $3\ \mathrm{XOR}\ 5 = 6$ (in binary: $011\ \mathrm{XOR}\ 101 = 110$).

翻译中…

Constraints

翻译中…
  • $1 \le T \le 2\times 10^5$
  • $1 \le N,K \le 10^9$
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

翻译中…

Here, $\text{case}_i$ denotes the $i$-th test case. Each test case is given in the following format:

翻译中…

N K

翻译中…

Output

翻译中…

For each test case, if there exist at least $K$ positive integers that are compatible with $N$, print the $K$-th smallest such integer. Otherwise, print -1.

翻译中…

样例

Input #1
4
2 1
2 2
1 7
20250126 191
Output #1
2
3
-1
20381694
Consider the case $N=2$. - When $X=1$, $X\ \mathrm{XOR}\ N = 3$ and the remainder of $X$ when divided by $N$ is $1$. Therefore, $1$ is not compatible with $N$. - When $X=2$, $X\ \mathrm{XOR}\ N = 0$ and the remainder of $X$ when divided by $N$ is $0$. Therefore, $2$ is compatible with $N$. - When $X=3$, $X\ \mathrm{XOR}\ N = 1$ and the remainder of $X$ when divided by $N$ is $1$. Therefore, $3$ is compatible with $N$. Hence, among the numbers that are compatible with $2$, the smallest is $2$ and the second smallest is $3$. Therefore, the answer to $\text{case}_1$ is $2$ and the answer to $\text{case}_2$ is $3$.
题目信息
题号
arc191_b
时间限制
2 s
内存限制
1024 MB

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