結果

問題 No.3 ビットすごろく
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-04-05 10:22:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 113 ms / 5,000 ms
コード長 409 bytes
コンパイル時間 404 ms
コンパイル使用メモリ 87,144 KB
実行使用メモリ 77,732 KB
最終ジャッジ日時 2023-08-28 22:28:16
合計ジャッジ時間 5,032 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,536 KB
testcase_01 AC 91 ms
71,584 KB
testcase_02 AC 89 ms
71,288 KB
testcase_03 AC 102 ms
77,264 KB
testcase_04 AC 90 ms
71,584 KB
testcase_05 AC 108 ms
77,316 KB
testcase_06 AC 103 ms
77,116 KB
testcase_07 AC 101 ms
77,176 KB
testcase_08 AC 107 ms
77,312 KB
testcase_09 AC 111 ms
77,576 KB
testcase_10 AC 110 ms
77,408 KB
testcase_11 AC 107 ms
76,932 KB
testcase_12 AC 108 ms
77,388 KB
testcase_13 AC 103 ms
77,240 KB
testcase_14 AC 113 ms
77,288 KB
testcase_15 AC 113 ms
77,636 KB
testcase_16 AC 110 ms
77,732 KB
testcase_17 AC 111 ms
76,968 KB
testcase_18 AC 100 ms
77,028 KB
testcase_19 AC 111 ms
77,316 KB
testcase_20 AC 92 ms
71,572 KB
testcase_21 AC 91 ms
71,516 KB
testcase_22 AC 112 ms
76,924 KB
testcase_23 AC 111 ms
77,304 KB
testcase_24 AC 110 ms
77,508 KB
testcase_25 AC 110 ms
77,308 KB
testcase_26 AC 90 ms
71,236 KB
testcase_27 AC 102 ms
76,860 KB
testcase_28 AC 112 ms
77,428 KB
testcase_29 AC 108 ms
77,584 KB
testcase_30 AC 90 ms
71,444 KB
testcase_31 AC 91 ms
71,288 KB
testcase_32 AC 111 ms
77,304 KB
権限があれば一括ダウンロードができます

ソースコード

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