#P11560. Minimum Operations to Equalize Two Numbers

    ID: 13651 Type: Default 1000ms 256MiB

Minimum Operations to Equalize Two Numbers

Minimum Operations to Equalize Two Numbers

Given two positive integers \(a\) and \(b\), you can perform one of the following operations in each move:

  • \(a \gets a \times 2\)
  • \(b \gets b - 1\)
  • \(b \gets b + 1\)

The goal is to make \(a = b\) with the minimum number of operations. One way to solve the problem is to consider the number of times you double \(a\) (say \(k\) times) so that \(a \times 2^k\) is as close as possible to \(b\), and then adjust \(b\) with the necessary increments or decrements. The total operations would be \(k + \left|a \times 2^k - b\right|\), and you should choose the minimum over all possible \(k\).

inputFormat

The input consists of a single line with two space-separated positive integers \(a\) and \(b\).

\(1 \leq a, b \leq 10^{9}\)

outputFormat

Output a single integer representing the minimum number of operations required to make \(a = b\).

sample

3 10
4