CodeTON Round 9 (Div. 1 + Div. 2, Rated, Prizes!)
C2
Shohag Loves XOR (Hard Version)
Codeforces
1800
原题
← C1D →
翻译中…

This is the hard version of the problem. The differences between the two versions are highlighted in bold. You can only make hacks if both versions of the problem are solved.

翻译中…

Shohag has two integers $x$ and $m$. Help him count the number of integers $1 \le y \le m$ such that $x \oplus y$ is divisible$^{\text{∗}}$ by either $x$, $y$, or both. Here $\oplus$ is the bitwise XOR operator.

翻译中…

$^{\text{∗}}$The number $a$ is divisible by the number $b$ if there exists an integer $c$ such that $a = b \cdot c$.

翻译中…

Input

翻译中…

The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

翻译中…

The first and only line of each test case contains two space-separated integers $x$ and $m$ ($1 \le x \le 10^6$, $1 \le m \le 10^{18}$).

翻译中…

It is guaranteed that the sum of $x$ over all test cases does not exceed $10^7$.

翻译中…

Output

翻译中…

For each test case, print a single integer — the number of suitable $y$.

翻译中…

Note

翻译中…

In the first test case, for $x = 7$, there are $3$ valid values for $y$ among the integers from $1$ to $m = 10$, and they are $1$, $7$, and $9$.

翻译中…
  • $y = 1$ is valid because $x \oplus y = 7 \oplus 1 = 6$ and $6$ is divisible by $y = 1$.
  • $y = 7$ is valid because $x \oplus y = 7 \oplus 7 = 0$ and $0$ is divisible by both $x = 7$ and $y = 7$.
  • $y = 9$ is valid because $x \oplus y = 7 \oplus 9 = 14$ and $14$ is divisible by $x = 7$.
翻译中…

样例

Input #1
5
7 10
2 3
6 4
1 6
4 1
Output #1
3
2
2
6
1
InputCopy OutputCopy
题目信息
题号
2039C2
时间限制
2 s
内存限制
256 MB
标签
位运算
暴力
数学
数论

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