増やす減らす二倍する 〜 横へな 2013.9.6 参考問題

問題

装置がある。
装置には数を表示する場所と、3つのボタン +1-1×2 がある。
表示されている数は、最初 0 になっている。
なにか決まった数(例えば 59 )があり、あなたはこの数を装置に表示したいと思っている。 目的の数値に到達するために、最低何回ボタンを押さなければならないのかを計算せよ。

たとえば 59 に到達するためには、 +1, +1, ×2, ×2, ×2, -1, ×2, ×2, -1 のように、 9 回の操作が必要となる。

入力と出力

入力文字列は 59 のように単なる整数 ( 10進数 ) であり、目的の数を示している。

出力も単なる整数を文字列にしたもので、 9 のような文字列を出力すればよい。

補足

サンプルデータ

# 入力 期待
0 59 9 +1 ×2 ×2 ×2 ×2 -1 ×2 ×2 -1
1 10 5 +1 +1 ×2 +1 ×2
2 11 6 +1 ×2 ×2 +1 ×2 +1
3 12 5 +1 +1 +1 ×2 ×2
4 13 6 +1 ×2 +1 ×2 ×2 +1
5 14 6 +1 +1 ×2 ×2 -1 ×2
6 15 6 +1 ×2 ×2 ×2 ×2 -1
7 16 5 +1 +1 ×2 ×2 ×2
8 17 6 +1 +1 ×2 ×2 ×2 +1
9 18 6 +1 +1 ×2 ×2 +1 ×2
10 27 8 +1 +1 ×2 ×2 -1 ×2 ×2 -1
11 28 7 +1 ×2 +1 ×2 +1 ×2 ×2
12 29 8 +1 ×2 ×2 ×2 ×2 -1 ×2 -1
13 30 7 +1 +1 ×2 ×2 ×2 -1 ×2
14 31 7 +1 ×2 ×2 ×2 ×2 ×2 -1
15 32 6 +1 +1 ×2 ×2 ×2 ×2
16 33 7 +1 ×2 ×2 ×2 ×2 ×2 +1
17 34 7 +1 ×2 ×2 ×2 ×2 +1 ×2
18 35 8 +1 +1 ×2 ×2 ×2 +1 ×2 +1
19 41 8 +1 +1 ×2 +1 ×2 ×2 ×2 +1
20 71 9 +1 +1 ×2 ×2 +1 ×2 ×2 ×2 -1
21 1023 12 +1 +1 ×2 ×2 ×2 ×2 ×2 ×2 ×2 ×2 ×2 -1
22 1024 11 +1 +1 ×2 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