HAP01 - Play with Binary Numbers

Let S be the binary representation of an integer. We define two functions a(i) and b(i) such that:

  • a(i) = Number of occurrences of '1' at odd positions of S,
  • b(i) = Number of occurrences of '1' at even positions of S.

For example: for integer 19, S = 10011, so a(19) = 2 and b(19) = 1.

Input

First line contains an integer T, the number of test cases. Then T lines follow. On each line, you will be given three integers M, N, K.

Output

For each test case output a single integer R, where R is the number of integers 'i' between M and N (both inclusive) such that absolute difference of a(i) and b(i) is equal to K. The answer to each each test case should be on a separate line.

Constraints

T <= 50
1 <= M < N <= 10^19
1 <= N - M <= 10^6
0 <= K <= 50

Example

Input:
1
1 10 2

Output:
2

Added by::-)
Date:2013-04-16
Time limit:1.201s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:own

hide comments
2014-02-12 21:38:49 I know nothing
awsome problem
AC in first go :)
2013-10-30 23:56:28 Martijn Muijsers
@Lakshay does it matter? You can switch meanings of a and b if you want.

Great prob to learn bitwise, that's what I used (brute-force still haha).
2013-08-11 10:30:22 Lakshay Singhal
odd pos counted from left or right?
2013-08-01 18:27:28 mystique_blue
hehe at a place i had a int i instead of long long ... earned a lot of tles today.
2013-07-31 11:18:16 Amit RC
I wasn't expecting brute force to be accepted..but it seems it easily is.
2013-05-26 17:36:51 abdelkarim
i think there some problem in spoj so i got compilation error 6 times !
2013-05-25 12:38:42 abdou_93
so easy ...:D
2013-05-22 21:35:37 tuhin
@ :-) AWESOME PROBLEM
LOVED SOLVING IT

Last edit: 2013-05-22 22:32:39
2013-05-19 19:08:16 Aradhya
@hindustan:: please provide a test case or plz check my submission id 9298476 :)
2013-04-18 16:11:41 :-)
@ RuPp$
Your program is giving wrong answer when K=0.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.