結果

問題 No.3 ビットすごろく
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-04-05 10:22:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 85 ms / 5,000 ms
コード長 409 bytes
コンパイル時間 571 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 70,400 KB
最終ジャッジ日時 2024-12-30 12:47:47
合計ジャッジ時間 3,983 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
n = int(input())
inf = 10**10
dp = [inf]*(n+2)
dp[1] = 1

q = deque([1])
while q:
    now = q.popleft()
    b = bin(now).count("1")
    if now+b <= n and dp[now+b] > dp[now]+1:
        dp[now+b] = dp[now]+1
        q.append(now+b)
    if now-b > 0 and dp[now-b] > dp[now]+1:
        dp[now-b] = dp[now]+1
        q.append(now-b)
ans = dp[n]
if ans == inf:
    ans = -1
print(ans)
0