Under construction

Still being built. Things may change

Not One Bit More

Time limit1sMemory limit128 MB

Summary
Count integers in [LO, HI] whose repeated popcount chain reaches 1 at exactly step X, with LO up to 1e18 and X up to 10.
Level

Medium6 of 10

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

Problem

Start with an integer N0>0N_0 > 0. Let N1N_1 be the number of ones in the binary representation of N0N_0. For example, if N0=27N_0 = 27, then N1=4N_1 = 4.

In general, let NiN_i be the number of ones in the binary representation of Ni−1N_{i-1}. This sequence always converges to one.

For any starting number N0N_0, let K(N0)K(N_0) be the smallest index ii such that Ni=1N_i = 1. For example, if N0=31N_0 = 31, then N1=5N_1 = 5, N2=2N_2 = 2, N3=1N_3 = 1, so K(31)=3K(31) = 3. In particular, K(1)=0K(1) = 0, since N0=1N_0 = 1 is already one.

Given a range of consecutive integers and a value XX, how many numbers in the range have a K(…)K(\ldots) value equal to XX?

Input

The input contains several test cases. Each test case is a single line with three integers:

LO HI X

where LOLO and HIHI (1≤LO≤HI≤10181 \le LO \le HI \le 10^{18}) are the lower and upper limits of a range of integers, and XX (0≤X≤100 \le X \le 10) is the target value for K(…)K(\ldots).

The input ends with a line containing three zeros, which is not processed.

Output

For each test case, print a single line with one integer: the number of integers in the range [LO,HI][LO, HI] (inclusive) whose K(…)K(\ldots) value equals XX.

Examples1

  1. Example 1

    Input
    31 31 3
    31 31 1
    27 31 1
    27 31 2
    1 4 1
    1023 1025 1
    1023 1025 2
    0 0 0
    
    Expected output
    1
    0
    0
    3
    2
    1
    1