結果

問題 No.3 ビットすごろく
ユーザー hiragn
提出日時 2022-12-23 02:50:09
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 35 ms / 5,000 ms
コード長 545 bytes
コンパイル時間 145 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-11-18 03:54:17
合計ジャッジ時間 2,311 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def main():
    n = int(input())

    dist = [0] * (n + 1)
    dist[1] = 1
    q = deque()
    q.append(1)
    while q:
        v = q.popleft()
        if v == n:
            exit(print(dist[v]))
        bc = v.bit_count()
        v1 = v + bc
        v2 = v - bc
        if v1 <= n and dist[v1] == 0:
            dist[v1] = dist[v] + 1
            q.append(v1)
        if v2 >= 1 and dist[v2] == 0:
            dist[v2] = dist[v] + 1
            q.append(v2)
    print(-1)


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