結果

問題 No.3 ビットすごろく
ユーザー masumasumath1masumasumath1
提出日時 2019-11-25 21:22:58
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 30 ms / 5,000 ms
コード長 689 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 10,788 KB
実行使用メモリ 8,964 KB
最終ジャッジ日時 2023-09-14 01:30:23
合計ジャッジ時間 1,846 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,524 KB
testcase_01 AC 16 ms
8,472 KB
testcase_02 AC 16 ms
8,616 KB
testcase_03 AC 18 ms
8,580 KB
testcase_04 AC 16 ms
8,652 KB
testcase_05 AC 24 ms
8,736 KB
testcase_06 AC 20 ms
8,620 KB
testcase_07 AC 17 ms
8,528 KB
testcase_08 AC 21 ms
8,688 KB
testcase_09 AC 24 ms
8,748 KB
testcase_10 AC 26 ms
8,708 KB
testcase_11 AC 24 ms
8,764 KB
testcase_12 AC 22 ms
8,748 KB
testcase_13 AC 18 ms
8,528 KB
testcase_14 AC 25 ms
8,832 KB
testcase_15 AC 28 ms
8,788 KB
testcase_16 AC 29 ms
8,964 KB
testcase_17 AC 27 ms
8,880 KB
testcase_18 AC 17 ms
8,512 KB
testcase_19 AC 26 ms
8,908 KB
testcase_20 AC 16 ms
8,584 KB
testcase_21 AC 15 ms
8,576 KB
testcase_22 AC 25 ms
8,676 KB
testcase_23 AC 27 ms
8,820 KB
testcase_24 AC 27 ms
8,764 KB
testcase_25 AC 30 ms
8,848 KB
testcase_26 AC 15 ms
8,508 KB
testcase_27 AC 18 ms
8,536 KB
testcase_28 AC 27 ms
8,744 KB
testcase_29 AC 23 ms
8,724 KB
testcase_30 AC 15 ms
8,496 KB
testcase_31 AC 17 ms
8,576 KB
testcase_32 AC 23 ms
8,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
def bfsp(i,k):
    if i+k <= N:
        if box[i+k] > 0:
            box[i+k] = min(box[i] + 1, box[i+k])
        else:
            box[i+k] = box[i] + 1
            q.append(i+k)


def bfsm(i,k):
    if i-k >= 1:
        if box[i-k] > 0:
            box[i-k] = min(box[i] + 1, box[i-k])
        else:
            box[i-k] = box[i] + 1
            q.append(i-k)

N = int(input())
box = [0 for _ in range(N+1)]
box[1] = 1
q = deque()
q.append(1)
while len(q) > 0:
    i = q.popleft()
    if box[i] <= 0:
        q.append(i+1)
        continue
    s = bin(i)
    k = s.count("1")
    bfsp(i,k)
    bfsm(i,k)
if box[N] > 0:
    print(box[N])
else:
    print(-1)
0