No.642 Two Operations No.1
Problem Statement
You have a natural number $N$ and an integer $X=1$, and you may perform the following $2$ operations any number of times in any order. Find the minimum number of operations required to make $X=N$. If it is impossible, output $-1$.
Operation 1: Decrease the value of $X$ by $1$
Operation 2: Multiply the value of $X$ by $2$
Input
$N$
$1\le N\le 10^9$
Output
Find the minimum number of operations to make $X=N$. If it is impossible, output $-1
$. Print a newline at the end.
Sample
Sample 1
Input
3
Output
3
You can make $X=N$ by performing the operations in the order Operation 2 $\rightarrow$ Operation 2 $\rightarrow$ Operation 1. The value of $X$ changes as follows.
$1\rightarrow 2\rightarrow 4\rightarrow 3$
Sample 2
Input
1
Output
0
There are cases where no operation is needed.
Sample 3
Input
256
Output
8
You can make $X=N$ by performing Operation 2 $8$ times.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
Pulmn
rullonz