#P12147. 【MX-X11-T1】「蓬莱人形 Round 1」仅此而已,就已经足够了

【MX-X11-T1】「蓬莱人形 Round 1」仅此而已,就已经足够了

Description

Define f(x)=x(x+2k)f(x) = x \oplus (x + 2^k), where \oplus denotes the bitwise XOR operation.

Given two integers nn and kk, compute the value of f(0)+f(1)+f(2)++f(n)f(0) + f(1) + f(2) + \cdots + f(n).

For knowledge about XOR operations, you may refer to the Wikipedia Page.

Input Format

Multiple test cases.

The first line contains a positive integer TT indicating the number of test cases.

Each of the next TT lines contains two integers nn and kk.

Output Format

Output TT lines, each containing the answer for the corresponding query.

9
3 0
15 0
9 4
3 6
17 28
9 16
8 23
15 11
4 11
12
80
160
256
4831838208
655360
75497472
32768
10240

Hint

Explanation #1

Sample 1 Explanation:

For the first test case:

  • f(0)=0(0+20)=1f(0) = 0 \oplus (0 + 2^0) = 1
  • f(1)=1(1+20)=3f(1) = 1 \oplus (1 + 2^0) = 3
  • f(2)=2(2+20)=1f(2) = 2 \oplus (2 + 2^0) = 1
  • f(3)=3(3+20)=7f(3) = 3 \oplus (3 + 2^0) = 7

Thus, the answer is 1+3+1+7=121 + 3 + 1 + 7 = 12.

Constraints

  • 20% of data: n,T5000n, T \leq 5000
  • Additional 20% of data: n105n \leq 10^5
  • Additional 30% of data: k=0k = 0
  • 100% of data: 1T1051 \leq T \leq 10^5, 0n<2290 \leq n < 2^{29}, 0k290 \leq k \leq 29

Translated by DeepSeek R1