No.3 Bit Sugoroku
Problem Statement
Carol is about to play a special game of sugoroku.
There are N squares arranged in a straight line, numbered in order from 1 to N.
Square 1 is the starting square, and the goal is the square on which N is written.
From a square, you can move "forward" or "backward" by exactly the number of 1 bits in the binary representation of the number written on that square.
(You cannot move to squares below 1 or to squares N+1 and above, and you can only reach the goal by landing exactly on N.)
Given a natural number N, find the minimum number of moves needed to reach the goal (moving onto the starting square also counts as a move).
If the goal cannot be reached, output -1.
The starting square may already be the goal.
Input
N
An integer $N(1 \le N \le 10000)$ representing the number of squares is given.
Output
The minimum number of moves, or -1.
Sample
Sample 1
Input
5
Output
4
The path taken is 1->2->3->5
.
Sample 2
Input
11
Output
9
1->2->3->5->7->10->8->9->11
Following this path reaches the goal in 9 moves.
Sample 3
Input
4
Output
-1
When there are only 4 squares, the goal cannot be reached.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。