結果

問題 No.3 ビットすごろく
ユーザー akanayaakanaya
提出日時 2020-03-20 20:40:10
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 50 ms / 5,000 ms
コード長 443 bytes
コンパイル時間 149 ms
コンパイル使用メモリ 10,896 KB
実行使用メモリ 9,400 KB
最終ジャッジ日時 2023-09-14 01:39:18
合計ジャッジ時間 2,427 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,976 KB
testcase_01 AC 20 ms
8,968 KB
testcase_02 AC 21 ms
9,044 KB
testcase_03 AC 27 ms
9,036 KB
testcase_04 AC 23 ms
9,044 KB
testcase_05 AC 36 ms
9,076 KB
testcase_06 AC 28 ms
9,156 KB
testcase_07 AC 24 ms
8,988 KB
testcase_08 AC 32 ms
9,108 KB
testcase_09 AC 41 ms
9,160 KB
testcase_10 AC 44 ms
9,156 KB
testcase_11 AC 42 ms
9,236 KB
testcase_12 AC 33 ms
9,112 KB
testcase_13 AC 24 ms
9,124 KB
testcase_14 AC 40 ms
9,168 KB
testcase_15 AC 47 ms
9,400 KB
testcase_16 AC 44 ms
9,316 KB
testcase_17 AC 48 ms
9,268 KB
testcase_18 AC 24 ms
8,956 KB
testcase_19 AC 49 ms
9,232 KB
testcase_20 AC 21 ms
8,980 KB
testcase_21 AC 19 ms
8,932 KB
testcase_22 AC 42 ms
9,316 KB
testcase_23 AC 49 ms
9,280 KB
testcase_24 AC 48 ms
9,284 KB
testcase_25 AC 50 ms
9,212 KB
testcase_26 AC 22 ms
8,920 KB
testcase_27 AC 28 ms
9,100 KB
testcase_28 AC 46 ms
9,244 KB
testcase_29 AC 41 ms
9,220 KB
testcase_30 AC 21 ms
9,108 KB
testcase_31 AC 20 ms
9,056 KB
testcase_32 AC 39 ms
9,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import queue

n = int(input())
q = queue.Queue()

mp = [10 ** 6] * n
q.put(0)
mp[0] = 1

while(not q.empty()):
    val = q.get()
    b = bin(val + 1).count('1')

    if val + b < n and mp[val] + 1 < mp[val + b]:
        mp[val + b] = mp[val] + 1
        q.put(val + b)
    if val - b > 0 and mp[val] + 1 < mp[val - b]:
        mp[val - b] = mp[val] + 1
        q.put(val - b)

if mp[n - 1] < 10 ** 6:
    print(mp[n - 1])
else:
    print(-1)

0