Codeforces Round 1083 (Div. 2)
G
Simons and Diophantus Equation
Codeforces
3000
原题
← F→
翻译中…

I walk alone, into distance, through the fading light, biding my time for the sunset's final light.— SHUN, CHAKA

翻译中…

Simons has given you two integers $n$ and $m$.

翻译中…

Count the number of ordered tuples $(i, j, k)$, such that:

翻译中…
  • $0\le i, j, k\le m$, and
  • There exist two integers $x$ and $y$, such that $(i \oplus j) \cdot x + (j \oplus k) \cdot y = n$, where $\oplus$ denotes the bitwise XOR operation.
翻译中…

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.

翻译中…

The only line contains two integers $n$ and $m$ ($1\le n\le 10^9$, $1\le m\le 3\cdot 10^5$) — the given integers.

翻译中…

It is guaranteed that the sum of $m$ over all test cases does not exceed $3\cdot10^5$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the number of ordered tuples $(i,j,k)$ that satisfy the condition.

翻译中…

Note

翻译中…

In the first test case, there are $18$ tuples that satisfy the conditions. For example:

翻译中…
  • $(2,1,2)$ is a valid tuple because the equation $(2\oplus 1)\cdot x+(1\oplus 2)\cdot y=3$ has an integer solution $x=3$, $y=-2$.
  • $(1,1,0)$ is also a valid tuple because the equation $(1\oplus 1)\cdot x+(1\oplus 0)\cdot y=3$ has an integer solution $x=100$, $y=3$.
  • $(2,0,2)$ is not a valid tuple because the equation $(2\oplus 0)\cdot x+(0\oplus 2)\cdot y=3$ has no integer solution.
  • $(1,1,1)$ is not a valid tuple because the equation $(1\oplus 1)\cdot x+(1\oplus 1)\cdot y=3$ has no integer solution.
  • $(3,2,1)$ is not a valid tuple because $3 \gt 2$.
翻译中…

样例

Input #1
5
3 2
4 6
1 1
7 20
720 2025
Output #1
18
254
6
5558
7864357450
InputCopy OutputCopy
题目信息
题号
2205G
时间限制
6 s
内存限制
512 MB
标签
位运算
暴力
数据结构
数学
数论

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