結果

問題 No.3 ビットすごろく
ユーザー work-furukawa
提出日時 2025-04-15 15:06:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 60 ms / 5,000 ms
コード長 530 bytes
コンパイル時間 437 ms
コンパイル使用メモリ 82,412 KB
実行使用メモリ 73,488 KB
最終ジャッジ日時 2025-04-15 15:06:43
合計ジャッジ時間 3,593 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
def popcount(x):
  binQ = bin(x)[2:]
  return binQ.count("1")

N = int(input())
INF=float('inf')
visited = [INF] * (N+1)
visited[1] = 1

que = deque()
que.append(1)
while que:
  q = que.popleft()
  cnt = popcount(q)
  if q+cnt <= N and visited[q+cnt] > visited[q] + 1:
    visited[q+cnt] = visited[q] + 1
    que.append(q+cnt)

  if 0 <= q-cnt and visited[q-cnt] > visited[q] + 1:
    visited[q-cnt] = visited[q] + 1
    que.append(q-cnt)

if visited[N] == INF:
  print(-1)
else:
  print(visited[N])
0