結果

問題 No.3 ビットすごろく
ユーザー ああいいああいい
提出日時 2021-12-04 17:13:25
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 41 ms / 5,000 ms
コード長 598 bytes
コンパイル時間 434 ms
コンパイル使用メモリ 10,880 KB
実行使用メモリ 8,868 KB
最終ジャッジ日時 2023-09-21 04:51:33
合計ジャッジ時間 2,834 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,596 KB
testcase_01 AC 19 ms
8,472 KB
testcase_02 AC 18 ms
8,500 KB
testcase_03 AC 23 ms
8,544 KB
testcase_04 AC 20 ms
8,620 KB
testcase_05 AC 30 ms
8,628 KB
testcase_06 AC 24 ms
8,620 KB
testcase_07 AC 21 ms
8,592 KB
testcase_08 AC 28 ms
8,528 KB
testcase_09 AC 33 ms
8,672 KB
testcase_10 AC 37 ms
8,708 KB
testcase_11 AC 32 ms
8,748 KB
testcase_12 AC 31 ms
8,684 KB
testcase_13 AC 22 ms
8,504 KB
testcase_14 AC 36 ms
8,800 KB
testcase_15 AC 40 ms
8,720 KB
testcase_16 AC 39 ms
8,744 KB
testcase_17 AC 40 ms
8,716 KB
testcase_18 AC 22 ms
8,472 KB
testcase_19 AC 41 ms
8,868 KB
testcase_20 AC 20 ms
8,576 KB
testcase_21 AC 19 ms
8,608 KB
testcase_22 AC 36 ms
8,808 KB
testcase_23 AC 40 ms
8,864 KB
testcase_24 AC 41 ms
8,776 KB
testcase_25 AC 40 ms
8,740 KB
testcase_26 AC 19 ms
8,584 KB
testcase_27 AC 23 ms
8,608 KB
testcase_28 AC 38 ms
8,704 KB
testcase_29 AC 33 ms
8,732 KB
testcase_30 AC 19 ms
8,504 KB
testcase_31 AC 19 ms
8,476 KB
testcase_32 AC 31 ms
8,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

N = int(input())
num = [N+1] * (N+1)
num[1] = 1

k = 1
while 2 ** k < N:
    k += 1
def keta(x):
    count = 0
    for i in range(k + 1):
        if x & (1 << i):
            count += 1
    return count
q = deque()
q.append(1)
while len(q):
    x = q.popleft()
    c = keta(x)
    if x - c > 0:
        if num[x] + 1 < num[x - c]:
            num[x-c] = num[x] + 1
            q.append(x - c)
    if x + c <= N:
        if num[x] + 1 < num[x + c]:
            num[x + c] = num[x] + 1
            q.append(x + c)
if num[N] == N+1:
    print(-1)
else:
    print(num[N])
0