Two Buttons at Monzón
Submit solution
Points:
100 (partial)
Time limit:
2.0s
Memory limit:
256M
Author:
Problem type
Allowed languages
C, C++
At a computing demonstration in the Monzón building, a screen initially displays the positive integer n. It has two buttons:
- Double: replace the current number x with 2x.
- Subtract: replace x with x - 1; this is allowed only if the result remains positive.
You want the screen to display m. Find the minimum number of button presses. Intermediate numbers may be larger than both n and m; the screen has no imposed upper bound.
Input
One line containing two distinct integers n and m.
Output
Print one integer: the minimum number of button presses.
Constraints
1 ≤ n, m ≤ 10 000; n ≠ m.
Ejemplo
| Input Example | Output Example |
|---|---|
| 5 8 | 2 |
| 9 3 | 6 |
Nota del Ejemplo 1: An optimal sequence is 5 → 4 → 8.
Subtracting six times reaches the target.
Source
Adapted for OiPR 2026. Codeforces 520B — Two Buttons.
Comments