結果

問題 No.3 ビットすごろく
ユーザー ryo_nryo_n
提出日時 2020-06-22 16:39:33
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 28 ms / 5,000 ms
コード長 671 bytes
コンパイル時間 87 ms
コンパイル使用メモリ 10,788 KB
実行使用メモリ 10,424 KB
最終ジャッジ日時 2023-09-14 01:46:50
合計ジャッジ時間 2,351 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,456 KB
testcase_01 AC 18 ms
8,428 KB
testcase_02 AC 19 ms
8,488 KB
testcase_03 AC 22 ms
8,848 KB
testcase_04 AC 19 ms
8,616 KB
testcase_05 AC 24 ms
9,412 KB
testcase_06 AC 21 ms
8,936 KB
testcase_07 AC 20 ms
8,784 KB
testcase_08 AC 22 ms
9,192 KB
testcase_09 AC 25 ms
9,828 KB
testcase_10 AC 26 ms
9,976 KB
testcase_11 AC 24 ms
9,628 KB
testcase_12 AC 23 ms
9,396 KB
testcase_13 AC 21 ms
8,776 KB
testcase_14 AC 26 ms
9,996 KB
testcase_15 AC 28 ms
10,376 KB
testcase_16 AC 26 ms
10,228 KB
testcase_17 AC 27 ms
10,372 KB
testcase_18 AC 21 ms
8,728 KB
testcase_19 AC 28 ms
10,348 KB
testcase_20 AC 19 ms
8,536 KB
testcase_21 AC 19 ms
8,456 KB
testcase_22 AC 25 ms
9,964 KB
testcase_23 AC 28 ms
10,356 KB
testcase_24 AC 28 ms
10,336 KB
testcase_25 AC 27 ms
10,424 KB
testcase_26 AC 18 ms
8,536 KB
testcase_27 AC 21 ms
8,892 KB
testcase_28 AC 26 ms
10,220 KB
testcase_29 AC 25 ms
9,644 KB
testcase_30 AC 19 ms
8,468 KB
testcase_31 AC 18 ms
8,472 KB
testcase_32 AC 24 ms
9,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    A = [[] for j in range(N)]

    for i in range(1, N):
        bitcnt = bin(i).count("1")
        if i + bitcnt <= N:
            A[i].append(i + bitcnt)

        if i - bitcnt > 0:
            A[i].append(i - bitcnt)

    q = collections.deque()
    q.append(1)
    d = [-1] * (N + 1)
    d[1] = 1

    while q:
        c = q.popleft()

        if c == N:
            break

        for n in A[c]:
            if d[n] == -1:
                d[n] = d[c] + 1
                q.append(n)

    print(d[N])


if __name__ == '__main__':
    main()
0