Easy Counting

3.3

6 votes
Combinatorics, Bit manipulation, Medium, Mathematics, Mathematics, Mathamatics
Problem

Tubby the doggu wants you to solve a problem for him/her.

You have to find number of numbers between L to R inclusive whose binary representation of length P contains equal number of 0s and 1s. It is guaranteed that Pceil(log2(R+1)) . Here ceil(x) denotes the largest integer greater than or equal to x.

Example : Binary representation of number 5 of length 5 is 00101.

Input Format

The first line will consist of the integer T denoting the number of test cases.

For each of the T test cases, there will be single line containing 3 integers L,R and P.

Output Format

For each of the T test cases, output a single integer denoting the number of numbers between L to R inclusive whose binary representation of length P contains equal number of 0s and 1s.

Constraints

1T300000

1LR1016

1P60

Pceil(log2(R+1))

WARNING: Large Input/output data, be careful with certain languages.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the first test case L=1, R=4 and P=4.

Binary representations of numbers of length 4 between 1 and 4 are 0001,0010,0011,0100.

We can easily see that only 3 has equal number of 0s and 1s in its binary representation.So ans=1.

Editor Image

?