Accurate Shots
Time limit5sMemory limit8 MB
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 to any integer divisible by .
The variable currently holds the integer , 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 . 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 and separated by a single space (). is the initial value of , and is the number that the final value of 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 that can be reached with that minimum number of shots and are divisible by , and the smallest such .
Hints
In the first example, the variable has value , which is 30. One shot is enough to make it a multiple of 7. There are two ways to do this: change it to or to . The first value is the minimum.
In the second example, the variable has value . The only way to make it a multiple of 41 is to change it to , which requires three shots.