Under construction

Still being built. Things may change

Accurate Shots

Time limit5sMemory limit8 MB

Summary
Flip the fewest bits of n's binary form so the result is divisible by m, then count the minimal-flip results and report the smallest.
Level

Medium7 of 10

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

Statement

Sophie plans to take over control of the world. Right now a central computer holds that control, but its software has critical bugs. To overthrow the computer's rule, Sophie only needs to set the value of its variable zz to any integer divisible by mm.

The variable zz currently holds the integer nn, written in binary without leading zeros. Sophie has built an ion cannon in her basement. Each shot flips one bit of her choice in the computer's memory, and she can make an accurate shot on any bit of the register that holds zz. The cannon can only flip bits that already exist in the binary representation. It cannot insert new bits anywhere, including at either end. Leading ones can be flipped to zeros. Sophie can shoot as many times as she likes, but each shot raises the chance of her plot being detected, so she wants to minimize the number of shots.

Input

The input is one line with two positive integers nn and mm separated by a single space (1≤n,m≤10151 \le n, m \le 10^{15}). nn is the initial value of zz, and mm is the number that the final value of zz must be a multiple of.

Output

Print three numbers separated by single spaces: the minimum number of shots needed to overthrow the computer's rule, the number of distinct n′n' that can be reached with that minimum number of shots and are divisible by mm, and the smallest such n′n'.

Hints

In the first example, the variable has value 11110(2)11110_{(2)}, which is 30. One shot is enough to make it a multiple of 7. There are two ways to do this: change it to 0‾1110(2)=14\underline{0}1110_{(2)} = 14 or to 1110‾0(2)=28111\underline{0}0_{(2)} = 28. The first value is the minimum.

In the second example, the variable has value 69=1000101(2)69 = 1000101_{(2)}. The only way to make it a multiple of 41 is to change it to 0=0‾0000‾00‾(2)0 = \underline{0}000\underline{0}0\underline{0}_{(2)}, which requires three shots.

Examples2

  1. Example 1

    Input
    30 7
    
    Expected output
    1 2 14
    
  2. Example 2

    Input
    69 41
    
    Expected output
    3 1 0