This page is still under construction.

Parts of this page are still being built. What you see may change.

Round Numbers

Interview

Time limit1sMemory limit128 MB

Summary
Count integers in [Start, Finish] whose binary form has at least as many zeroes as ones.
Level

Medium6 of 10

Topics
Dynamic programming, Bit manipulation, Combinatorics, Math
Solved
No attempts yet

Problem

Cows have no fingers or thumbs, so they cannot play 'Rock, Paper, Scissors' to make arbitrary decisions such as who gets to be milked first. They can't even flip a coin, because it is so hard to toss one with hooves.

So they have resorted to matching round numbers. The first cow picks an integer less than two billion, and the second cow does the same. If both numbers are round numbers, the first cow wins; otherwise the second cow wins.

A positive integer NN is a round number if its binary representation (written without leading zeroes) has as many or more zeroes than ones. For example, 99 in binary is 10011001; it has two zeroes and two ones, so 99 is a round number. The integer 2626 is 1101011010 in binary; it has two zeroes and three ones, so it is not a round number.

Converting numbers to binary takes cows a while, so deciding the winner also takes a while. Bessie thinks she will have an advantage if she knows how many round numbers lie in a given range.

Write a program that reports how many round numbers appear in the inclusive range from StartStart to FinishFinish (1≤Start<Finish≤2,000,000,0001 \le Start < Finish \le 2{,}000{,}000{,}000).

Input

  • Line 1: Two space-separated integers StartStart and FinishFinish.

Output

  • Line 1: A single integer, the count of round numbers in the inclusive range Start…FinishStart \ldots Finish.

Hint

The table below shows, for each integer from 22 to 1212, whether it is a round number. (Columns: decimal, binary, zeroes x0 + ones x1, verdict.)

 2    10  1x0 + 1x1  ROUND
 3    11  0x0 + 2x1  NOT round
 4   100  2x0 + 1x1  ROUND
 5   101  1x0 + 2x1  NOT round
 6   110  1x0 + 2x1  NOT round
 7   111  0x0 + 3x1  NOT round
 8  1000  3x0 + 1x1  ROUND
 9  1001  2x0 + 2x1  ROUND
10  1010  2x0 + 2x1  ROUND
11  1011  1x0 + 3x1  NOT round
12  1100  2x0 + 2x1  ROUND

Examples3

  1. Example 1

    Input
    2 12
    
    Expected output
    6
    
  2. Example 2

    Input
    1 2
    
    Expected output
    1
    
  3. Example 3

    Input
    7 9
    
    Expected output
    2