import random
cache = {}
def popcount(n):
res = 0
while n:
res += 1
n &= n - 1
return res
def solve(n, k):
if k == 0:
return 0
if k == 1:
return popcount(n) & 1
if k % 2 == 1:
t = solve(n, k - 1)
x = popcount((k - 1) ^ (n + k - 1)) & 1
return t + x
if (n, k) in cache:
return cache[(n, k)]
if n % 2 == 0:
one_cell = 2
zero_cell = 0
cnt1 = solve(n // 2, k // 2)
cnt0 = k // 2 - cnt1
else:
one_cell = 0
zero_cell = 1
cnt1 = solve(n // 2, k // 2) + solve(n // 2 + 1, k // 2)
cnt0 = k - cnt1
res = one_cell * cnt1 + zero_cell * cnt0
cache[(n, k)] = res
return res
t = int(input())
for _ in range(t):
cache.clear()
n, k = map(int, input().split())
print(solve(n, k))
228A - Is your horseshoe on the other hoof | 122A - Lucky Division |
1611C - Polycarp Recovers the Permutation | 432A - Choosing Teams |
758A - Holiday Of Equality | 1650C - Weight of the System of Nested Segments |
1097A - Gennady and a Card Game | 248A - Cupboards |
1641A - Great Sequence | 1537A - Arithmetic Array |
1370A - Maximum GCD | 149A - Business trip |
34A - Reconnaissance 2 | 59A - Word |
462B - Appleman and Card Game | 1560C - Infinity Table |
1605C - Dominant Character | 1399A - Remove Smallest |
208A - Dubstep | 1581A - CQXYM Count Permutations |
337A - Puzzles | 495A - Digital Counter |
796A - Buying A House | 67A - Partial Teacher |
116A - Tram | 1472B - Fair Division |
1281C - Cut and Paste | 141A - Amusing Joke |
112A - Petya and Strings | 677A - Vanya and Fence |