結果

問題 No.3 ビットすごろく
ユーザー konchanksukonchanksu
提出日時 2020-04-28 21:04:20
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 102 ms / 5,000 ms
コード長 562 bytes
コンパイル時間 97 ms
コンパイル使用メモリ 10,940 KB
実行使用メモリ 9,584 KB
最終ジャッジ日時 2023-09-14 01:43:00
合計ジャッジ時間 3,054 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
9,036 KB
testcase_01 AC 18 ms
9,000 KB
testcase_02 AC 18 ms
9,064 KB
testcase_03 AC 36 ms
9,052 KB
testcase_04 AC 23 ms
8,976 KB
testcase_05 AC 57 ms
9,224 KB
testcase_06 AC 38 ms
9,120 KB
testcase_07 AC 30 ms
9,124 KB
testcase_08 AC 50 ms
9,136 KB
testcase_09 AC 70 ms
9,324 KB
testcase_10 AC 86 ms
9,336 KB
testcase_11 AC 67 ms
9,292 KB
testcase_12 AC 57 ms
9,232 KB
testcase_13 AC 33 ms
9,020 KB
testcase_14 AC 79 ms
9,408 KB
testcase_15 AC 96 ms
9,412 KB
testcase_16 AC 90 ms
9,432 KB
testcase_17 AC 95 ms
9,584 KB
testcase_18 AC 31 ms
9,012 KB
testcase_19 AC 96 ms
9,464 KB
testcase_20 AC 24 ms
9,024 KB
testcase_21 AC 20 ms
9,068 KB
testcase_22 AC 84 ms
9,448 KB
testcase_23 AC 98 ms
9,532 KB
testcase_24 AC 99 ms
9,424 KB
testcase_25 AC 102 ms
9,412 KB
testcase_26 AC 20 ms
8,952 KB
testcase_27 AC 39 ms
9,092 KB
testcase_28 AC 94 ms
9,416 KB
testcase_29 AC 70 ms
9,408 KB
testcase_30 AC 19 ms
9,000 KB
testcase_31 AC 19 ms
8,940 KB
testcase_32 AC 63 ms
9,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import queue

N = int(input())

if N == 1:
    print(1)
    exit()

Q = queue.Queue()
Q.put(1)
already = [False for i in range(N + 1)]
dp = [float('inf') for i in range(N + 1)]
dp[1] = 1


while Q.qsize():
    now = Q.get()
    
    if already[now]:
        continue
    already[now] = True
    
    tmp = 0
    for i in bin(now)[2:]:
        tmp += int(i)
        
    for i in [tmp, -tmp]:
        if i + now > 1 and i + now <= N:
            dp[i + now] = min(1 + dp[now], dp[i + now])
            Q.put(i + now)

print(dp[N] if dp[N] != float('inf') else -1)
0