#P1918C. XOR-distance

XOR-distance

XOR-distance

题面翻译

给定三个正整数 a,b,ra,b,r,请你选择一个非负整数 x[0,r]x\in[0,r],使 (ax)(bx)| (a\oplus x)-(b\oplus x)| 最小。其中 \oplus 为按位异或运算符,m|m| 表示 m 的绝对值。

多测,a,b,r1018a,b,r\le10^{18}

提示:c++ 中,请使用 (1ll<<k) 来计算一个大于 int 范围的位运算值。

题目描述

You are given integers a a , b b , r r . Find the smallest value of (ax)(bx) |({a \oplus x}) - ({b \oplus x})| among all 0xr 0 \leq x \leq r .

\oplus is the operation of bitwise XOR, and y |y| is absolute value of y y .

输入格式

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

Each test case contains integers a a , b b , r r ( 0a,b,r1018 0 \le a, b, r \le 10^{18} ).

输出格式

For each test case, output a single number — the smallest possible value.

样例 #1

样例输入 #1

10
4 6 0
0 3 2
9 6 10
92 256 23
165 839 201
1 14 5
2 7 2
96549 34359 13851
853686404475946 283666553522252166 127929199446003072
735268590557942972 916721749674600979 895150420120690183

样例输出 #1

2
1
1
164
542
5
3
37102
27934920819538516
104449824168870225

提示

In the first test, when r=0 r = 0 , then x x is definitely equal to 0 0 , so the answer is 4060=46=2 |{4 \oplus 0} - {6 \oplus 0}| = |4 - 6| = 2 .

In the second test:

  • When x=0 x = 0 , 0030=03=3 |{0 \oplus 0} - {3 \oplus 0}| = |0 - 3| = 3 .
  • When x=1 x = 1 , 0131=12=1 |{0 \oplus 1} - {3 \oplus 1}| = |1 - 2| = 1 .
  • When x=2 x = 2 , 0232=21=1 |{0 \oplus 2} - {3 \oplus 2}| = |2 - 1| = 1 .

Therefore, the answer is 1 1 .

In the third test, the minimum is achieved when x=1 x = 1 .