問題一覧 > 通常問題

No.3 Bit Sugoroku

レベル : / 実行時間制限 : 1ケース 5.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 1115
作問者 : yuki2006
お気に入りにしたユーザー ProblemId : 11 / 自分の提出
問題文最終更新日: 2024-12-14 23:56:17
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 3 (順位表) の他の問題:

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もしくは右上の雲マークをクリックしてアカウントを作成してください。