結果

問題 No.3 ビットすごろく
ユーザー DialBirdDialBird
提出日時 2019-07-19 12:02:05
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 30 ms / 5,000 ms
コード長 735 bytes
コンパイル時間 94 ms
コンパイル使用メモリ 11,024 KB
実行使用メモリ 9,336 KB
最終ジャッジ日時 2023-09-14 01:23:32
合計ジャッジ時間 2,246 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,508 KB
testcase_01 AC 21 ms
8,584 KB
testcase_02 AC 20 ms
8,576 KB
testcase_03 AC 22 ms
8,604 KB
testcase_04 AC 19 ms
8,696 KB
testcase_05 AC 24 ms
8,628 KB
testcase_06 AC 21 ms
8,676 KB
testcase_07 AC 20 ms
8,556 KB
testcase_08 AC 24 ms
8,628 KB
testcase_09 AC 26 ms
9,208 KB
testcase_10 AC 27 ms
9,316 KB
testcase_11 AC 25 ms
8,672 KB
testcase_12 AC 24 ms
8,800 KB
testcase_13 AC 20 ms
8,608 KB
testcase_14 AC 27 ms
9,172 KB
testcase_15 AC 30 ms
9,192 KB
testcase_16 AC 28 ms
9,244 KB
testcase_17 AC 30 ms
9,196 KB
testcase_18 AC 21 ms
8,652 KB
testcase_19 AC 29 ms
9,260 KB
testcase_20 AC 19 ms
8,532 KB
testcase_21 AC 18 ms
8,528 KB
testcase_22 AC 27 ms
9,196 KB
testcase_23 AC 29 ms
9,188 KB
testcase_24 AC 30 ms
9,184 KB
testcase_25 AC 29 ms
9,268 KB
testcase_26 AC 18 ms
8,528 KB
testcase_27 AC 21 ms
8,624 KB
testcase_28 AC 29 ms
9,164 KB
testcase_29 AC 26 ms
9,336 KB
testcase_30 AC 19 ms
8,652 KB
testcase_31 AC 19 ms
8,644 KB
testcase_32 AC 25 ms
8,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def bit_count(n):
    cnt = 0
    while 0 < n:
        n &= n-1
        cnt += 1
    return cnt


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 = bit_count(pos)

        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