結果

問題 No.3 ビットすごろく
ユーザー NoaSaberNoaSaber
提出日時 2021-03-07 10:26:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 85 ms / 5,000 ms
コード長 563 bytes
コンパイル時間 143 ms
コンパイル使用メモリ 82,100 KB
実行使用メモリ 76,928 KB
最終ジャッジ日時 2024-04-17 08:17:41
合計ジャッジ時間 3,274 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
53,888 KB
testcase_01 AC 42 ms
53,760 KB
testcase_02 AC 42 ms
53,504 KB
testcase_03 AC 60 ms
68,608 KB
testcase_04 AC 43 ms
55,296 KB
testcase_05 AC 71 ms
74,284 KB
testcase_06 AC 63 ms
69,632 KB
testcase_07 AC 56 ms
65,408 KB
testcase_08 AC 70 ms
72,704 KB
testcase_09 AC 78 ms
76,828 KB
testcase_10 AC 80 ms
76,716 KB
testcase_11 AC 76 ms
76,632 KB
testcase_12 AC 70 ms
73,976 KB
testcase_13 AC 58 ms
67,200 KB
testcase_14 AC 77 ms
76,544 KB
testcase_15 AC 79 ms
76,928 KB
testcase_16 AC 81 ms
76,872 KB
testcase_17 AC 80 ms
76,560 KB
testcase_18 AC 58 ms
66,176 KB
testcase_19 AC 82 ms
76,928 KB
testcase_20 AC 44 ms
55,168 KB
testcase_21 AC 40 ms
53,608 KB
testcase_22 AC 79 ms
76,800 KB
testcase_23 AC 82 ms
76,908 KB
testcase_24 AC 80 ms
76,844 KB
testcase_25 AC 76 ms
76,800 KB
testcase_26 AC 40 ms
53,632 KB
testcase_27 AC 58 ms
67,456 KB
testcase_28 AC 85 ms
76,544 KB
testcase_29 AC 81 ms
76,800 KB
testcase_30 AC 45 ms
54,016 KB
testcase_31 AC 40 ms
53,888 KB
testcase_32 AC 78 ms
76,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N=int(input())
dp=[float("inf")]*(N+1)
dp[1]=1;dp[0]=-1
from collections import deque
q=deque([1])
while q:
    i=q.popleft()
    if dp[i]>=1:
        a=str(bin(i))
        a=a.count("1")
        try:
            if dp[i+a]==float("inf"):
                q.append(i+a)
            dp[i+a]=min(dp[i]+1,dp[i+a])
        except IndexError:
            pass
        try:
            if dp[i-a]==float("inf"):
                q.append(i-a)
            dp[i-a]=min(dp[i]+1,dp[i-a])
        except IndexError:
            pass
print(dp[N] if dp[N]!=float("inf") else -1)
0