0% found this document useful (0 votes)
12 views2 pages

Prob-Count Periodic Numbers

Uploaded by

aashnangoyal12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Prob-Count Periodic Numbers

Uploaded by

aashnangoyal12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Count Periodic Numbers

Problem Statement
A positive integer n is said to be periodic if it satisfies the following property:
Take the binary representation of n without leading zeros. Then create an array which
contains the length of consecutive runs of equal bits in this binary representation. All the
elements of this array should be equal. If there are two unequal elements in this array,
then n is not periodic.

Examples
ˆ Suppose n = 3. Its binary representation is 11. The array created would be {2},
which corresponds to the fact that there are two equal bits at the beginning. This
is periodic.

ˆ Suppose n = 51. Its binary representation is 110011. The array created would
be {2, 2, 2}, which corresponds to the fact that there are two equal bits at the
beginning, then the next two are equal, and then the next two are equal. This is
also periodic.

ˆ Suppose n = 103. Its binary representation is 1100111. The array created would
be {2, 2, 3}, which corresponds to the fact that there are two equal bits at the
beginning, then the next two are equal, and then the next three are equal. This is
not periodic because the array contains two different values (2 and 3).

You are given two integers L, R. Find the number of integers in the range [L, R] (both
inclusive) that are periodic.

Input Format
ˆ The first line of the input contains an integer T denoting the number of test cases.

ˆ Each test case consists of one line containing two space-separated integers L, R.

Output Format
For each test case, output a single line containing an integer corresponding to the number
of periodic numbers in the range [L, R].

1
Constraints
ˆ 1 ≤ T ≤ 105

ˆ 1 ≤ L, R ≤ 109

Example Input
2
3 3
1 10

Example Output
1
6

Explanation
Testcase 1: The only number between L and R is 3, which is periodic. Hence the answer
is 1.
Testcase 2: The periodic numbers between 1 and 10 are 1, 2, 3, 5, 7, 10. Since there
are six of them, the answer is 6.

You might also like