This page is still under construction.

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

Colored zeros

Time limit2sMemory limit512 MB

Summary
Count how many zero bits get marked when writing 1 to n in binary and flagging every k-th zero in each row.
Level

Hard8 of 10

Topics
Math, Bit manipulation, Dynamic programming, Number theory
Solved
No attempts yet

Problem

Толик has just learned that binary notation exists. Delighted by this, he wrote down the binary forms of the numbers 1, 2, ..., nn in a column. This gave the numbers 1, 10, 11, 100, 101, 110, 111, ...

After that he erased all the written ones and started studying the positions of the zeros. He chose a number kk and in each row, going from left to right, marked in red every kk-th zero, starting with the first. Thus the zeros numbered 1,k+1,2k+1,…1, k + 1, 2k + 1, \ldots were marked. For example, if k=2k = 2, n=56n = 56, the rows would look like this:

11 0 0 01 1 1 11 0 1 1 01 1 1 0 11 0 0 1 0 01 0 1 0 1 11 1 0 0 1 0
1 01 0 0 11 0 0 0 01 0 1 1 11 1 1 1 01 0 0 1 0 11 0 1 1 0 01 1 0 0 1 1
1 11 0 1 01 0 0 0 11 1 0 0 01 1 1 1 11 0 0 1 1 01 0 1 1 0 11 1 0 1 0 0
1 0 01 0 1 11 0 0 1 01 1 0 0 11 0 0 0 0 01 0 0 1 1 11 0 1 1 1 01 1 0 1 0 1
1 0 11 1 0 01 0 0 1 11 1 0 1 01 0 0 0 0 11 0 1 0 0 01 0 1 1 1 11 1 0 1 1 0
1 1 01 1 0 11 0 1 0 01 1 0 1 11 0 0 0 1 01 0 1 0 0 11 1 0 0 0 01 1 0 1 1 1
1 1 11 1 1 01 0 1 0 11 1 1 0 01 0 0 0 1 11 0 1 0 1 01 1 0 0 0 11 1 1 0 0 0

(red zeros are shown in bold and underlined)

Now Толик wonders how many zeros he marked. Help him count them.

Input

The input file contains the numbers nn and kk (1≤n<2311 \le n < 2^{31}, 1≤k≤301 \le k \le 30).

Output

The output file must contain one number: the number of red zeros.

Examples2

  1. Example 1

    Input
    4 1
    
    Expected output
    3
    
  2. Example 2

    Input
    56 2
    
    Expected output
    74