結果

問題 No.3 ビットすごろく
ユーザー GrayCoder
提出日時 2018-06-28 20:16:35
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 39 ms / 5,000 ms
コード長 735 bytes
コンパイル時間 90 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-07-01 09:04:14
合計ジャッジ時間 2,107 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
from sys import stdin, stdout
input = lambda: stdin.readline().rstrip()
write = stdout.write

def bitCount(i):
    ret = 0
    while i:
        i &= i - 1
        ret += 1
    return ret

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

    flag = [None] * (N + 1)
    flag[1] = 1
    que = deque([(1, 1)])
    while que:
        cnt, n = que.popleft()
        bit = bitCount(n)
        a, b = n + bit, n - bit
        if a <= N or b >= 0:
            cnt += 1
        if a <= N and flag[a] is None:
            que.append((cnt, a))
            flag[a] = cnt
        if b > 0 and flag[b] is None:
            que.append((cnt, b))
            flag[b] = cnt

    print(-1) if flag[-1] is None else print(flag[-1])

main()
0