結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-02-25 00:09:34 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 75 ms / 5,000 ms | 
| コード長 | 415 bytes | 
| コンパイル時間 | 257 ms | 
| コンパイル使用メモリ | 82,096 KB | 
| 実行使用メモリ | 76,688 KB | 
| 最終ジャッジ日時 | 2024-09-13 06:19:07 | 
| 合計ジャッジ時間 | 3,441 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
from collections import deque
n = int(input())
dp = [None for _ in range(n + 1)]
dq = deque()
src = 1
dp[src] = 0
dq.append(src)
while len(dq) > 0:
  cur = dq.popleft()
  cnt = len([c for c in bin(cur) if c == '1'])
  for dir in [-1, 1]:
    nxt = cur + dir * cnt
    if 1 <= nxt <= n and dp[nxt] is None:
      dp[nxt] = dp[cur] + 1
      dq.append(nxt)
if dp[n] is None:
  print("-1")
else:
  print(dp[-1] + 1)
            
            
            
        