p-binary
Contest: Technocup 2020 - Elimination Round 2
Difficulty rating: 1600
Tags: bitmasks, brute force, math

Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $$$p$$$ (which may be positive, negative, or zero). To combine their tastes, they invented $$$p$$$-binary numbers of the form $$$2^x + p$$$, where $$$x$$$ is a non-negative integer.

For example, some $$$-9$$$-binary ("minus nine" binary) numbers are: $$$-8$$$ (minus eight), $$$7$$$ and $$$1015$$$ ($$$-8=2^0-9$$$, $$$7=2^4-9$$$, $$$1015=2^{10}-9$$$).

The boys now use $$$p$$$-binary numbers to represent everything. They now face a problem: given a positive integer $$$n$$$, what's the smallest number of $$$p$$$-binary numbers (not necessarily distinct) they need to represent $$$n$$$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.

For example, if $$$p=0$$$ we can represent $$$7$$$ as $$$2^0 + 2^1 + 2^2$$$.

And if $$$p=-9$$$ we can represent $$$7$$$ as one number $$$(2^4-9)$$$.

Note that negative $$$p$$$-binary numbers are allowed to be in the sum (see the Notes section for an example).

Input format:
The only line contains two integers $$$n$$$ and $$$p$$$ ($$$1 \leq n \leq 10^9$$$, $$$-1000 \leq p \leq 1000$$$).

Output format:
If it is impossible to represent $$$n$$$ as the sum of any number of $$$p$$$-binary numbers, print a single integer $$$-1$$$. Otherwise, print the smallest possible number of summands.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
$$$0$$$-binary numbers are just regular binary powers, thus in the first sample case we can represent $$$24 = (2^4 + 0) + (2^3 + 0)$$$.

In the second sample case, we can represent $$$24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$$$.

In the third sample case, we can represent $$$24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$$$. Note that repeated summands are allowed.

In the fourth sample case, we can represent $$$4 = (2^4 - 7) + (2^1 - 7)$$$. Note that the second summand is negative, which is allowed.

In the fifth sample case, no representation is possible.

Example input:
24 0

Example output:
2

Example input:
24 1

Example output:
3

Editorial:
Suppose we want to represent $$$n$$$ as the sum of $$$k$$$ $$$p$$$-binary numbers. We must have $$$n = \sum_{i = 1}^k (2^{x_i} + p)$$$ for a suitable choice of $$$x_1, \ldots, x_k$$$. Moving all $$$p$$$'s to the left-hand side, we must have $$$n - kp = \sum_{i=1}^k 2^{x_i}$$$. In particular, $$$n - kp$$$ has to be at least $$$k$$$.
Consider the binary representation of $$$n - kp$$$. If it has more than $$$k$$$ bits equal to $$$1$$$, there is no way we can split it into $$$k$$$ powers of two. Otherwise, we can start by taking the binary representation, and if it contains less than $$$k$$$ powers, we can always split larger powers into two smaller ones.
We can now check all values of $$$k$$$ starting from the smallest. If $$$n \geq 31p + 31$$$, then the answer will not exceed $$$31$$$ since $$$n - 31p$$$ is less than $$$2^{31}$$$, hence is always representable with $$$31$$$ powers. Otherwise, we have $$$n - 31(p + 1) < 0$$$. Since $$$n > 0$$$, it means that $$$p + 1 < 0$$$, and $$$n - kp < k$$$ for all $$$k > 31$$$, thus the answer does not exist.