結果

問題 No.3 ビットすごろく
ユーザー DialBirdDialBird
提出日時 2019-07-19 11:56:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 29 ms / 5,000 ms
コード長 765 bytes
コンパイル時間 181 ms
コンパイル使用メモリ 10,984 KB
実行使用メモリ 8,972 KB
最終ジャッジ日時 2023-09-14 01:23:16
合計ジャッジ時間 2,290 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,588 KB
testcase_01 AC 19 ms
8,672 KB
testcase_02 AC 19 ms
8,592 KB
testcase_03 AC 22 ms
8,668 KB
testcase_04 AC 19 ms
8,448 KB
testcase_05 AC 24 ms
8,820 KB
testcase_06 AC 21 ms
8,620 KB
testcase_07 AC 20 ms
8,588 KB
testcase_08 AC 23 ms
8,580 KB
testcase_09 AC 26 ms
8,832 KB
testcase_10 AC 27 ms
8,724 KB
testcase_11 AC 25 ms
8,628 KB
testcase_12 AC 24 ms
8,648 KB
testcase_13 AC 20 ms
8,676 KB
testcase_14 AC 27 ms
8,712 KB
testcase_15 AC 29 ms
8,840 KB
testcase_16 AC 29 ms
8,780 KB
testcase_17 AC 29 ms
8,872 KB
testcase_18 AC 22 ms
8,652 KB
testcase_19 AC 29 ms
8,924 KB
testcase_20 AC 19 ms
8,660 KB
testcase_21 AC 19 ms
8,584 KB
testcase_22 AC 27 ms
8,876 KB
testcase_23 AC 29 ms
8,884 KB
testcase_24 AC 29 ms
8,924 KB
testcase_25 AC 29 ms
8,972 KB
testcase_26 AC 18 ms
8,536 KB
testcase_27 AC 20 ms
8,668 KB
testcase_28 AC 28 ms
8,784 KB
testcase_29 AC 26 ms
8,824 KB
testcase_30 AC 19 ms
8,588 KB
testcase_31 AC 19 ms
8,640 KB
testcase_32 AC 25 ms
8,756 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 = [0 for _ in range(N+1)]
    dp[1] = 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 dp[pos - step] == 0:
            dp[pos-step] = cnt + 1
            q.append((pos-step, cnt+1))

        if (pos + step) <= N and dp[pos + step] == 0:
            dp[pos+step] = cnt + 1
            q.append((pos+step, cnt+1))
    else:
        print(-1)


main()
0