結果

問題 No.3 ビットすごろく
ユーザー DialBirdDialBird
提出日時 2019-07-19 12:03:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 27 ms / 5,000 ms
コード長 642 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 10,804 KB
実行使用メモリ 9,420 KB
最終ジャッジ日時 2023-09-14 01:22:45
合計ジャッジ時間 2,027 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,504 KB
testcase_01 AC 18 ms
8,544 KB
testcase_02 AC 18 ms
8,520 KB
testcase_03 AC 20 ms
8,756 KB
testcase_04 AC 19 ms
8,520 KB
testcase_05 AC 23 ms
8,716 KB
testcase_06 AC 21 ms
8,644 KB
testcase_07 AC 18 ms
8,720 KB
testcase_08 AC 21 ms
8,872 KB
testcase_09 AC 23 ms
9,224 KB
testcase_10 AC 25 ms
9,416 KB
testcase_11 AC 23 ms
8,704 KB
testcase_12 AC 21 ms
8,864 KB
testcase_13 AC 19 ms
8,744 KB
testcase_14 AC 24 ms
9,420 KB
testcase_15 AC 26 ms
9,300 KB
testcase_16 AC 25 ms
9,368 KB
testcase_17 AC 26 ms
9,392 KB
testcase_18 AC 20 ms
8,620 KB
testcase_19 AC 27 ms
9,264 KB
testcase_20 AC 18 ms
8,596 KB
testcase_21 AC 18 ms
8,516 KB
testcase_22 AC 24 ms
9,276 KB
testcase_23 AC 26 ms
9,396 KB
testcase_24 AC 26 ms
9,392 KB
testcase_25 AC 26 ms
9,244 KB
testcase_26 AC 18 ms
8,516 KB
testcase_27 AC 20 ms
8,540 KB
testcase_28 AC 27 ms
9,276 KB
testcase_29 AC 24 ms
9,420 KB
testcase_30 AC 18 ms
8,560 KB
testcase_31 AC 19 ms
8,504 KB
testcase_32 AC 22 ms
8,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def main():
    """ main """
    N = int(input())
    dp = set()
    dp.add(1)
    q = deque([(1, 1)])
    # queueが空になるまでloopを回し、到達すればbreak and return
    while q:
        pos, cnt = q.popleft()

        if pos == N:
            print(cnt)
            break

        step = bin(pos).count('1')

        if 1 < (pos - step) and pos-step not in dp:
            dp.add(pos-step)
            q.append((pos-step, cnt+1))

        if (pos + step) <= N and pos + step not in dp:
            dp.add(pos+step)
            q.append((pos+step, cnt+1))
    else:
        print(-1)


main()
0