結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  maburoe | 
| 提出日時 | 2019-05-28 15:36:02 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 242 ms / 5,000 ms | 
| コード長 | 762 bytes | 
| コンパイル時間 | 92 ms | 
| コンパイル使用メモリ | 12,672 KB | 
| 実行使用メモリ | 11,904 KB | 
| 最終ジャッジ日時 | 2024-07-01 09:20:23 | 
| 合計ジャッジ時間 | 4,760 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
import sys
sys.setrecursionlimit(10000)
goal = int(input())
stepped = dict()
maybe_answers = set()
def step(position, count):
    if position in stepped:
        if count < stepped[position]:
            stepped[position] = count
        else:
            return
    else:
        stepped[position] = count
    if position not in range(1, goal + 1):
        return
    if len(maybe_answers) > 0 and count > min(maybe_answers):
        return
    if position == goal:
        maybe_answers.add(count)
        return
    position_bin = bin(position)
    step(position + position_bin.count('1'), count + 1)
    step(position - position_bin.count('1'), count + 1)
step(1, 1)
if len(maybe_answers) == 0:
    print('-1')
else:
    print(min(maybe_answers))
            
            
            
        