結果

問題 No.3 ビットすごろく
ユーザー r_ishida
提出日時 2025-05-05 19:10:50
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 55 ms / 5,000 ms
コード長 797 bytes
コンパイル時間 453 ms
コンパイル使用メモリ 12,252 KB
実行使用メモリ 10,496 KB
最終ジャッジ日時 2025-05-05 19:10:53
合計ジャッジ時間 2,971 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

from collections import deque

n = I()

if n == 1:
    print(1)
    exit()

visited = [False] * (n + 1)
visited[1] = True

q = deque()
q.append((1, 1))

while q:
    x, d = q.popleft()
    move = bin(x).count('1')

    for next_pos in (x-move, x+move):
        if 1 <= next_pos <= n and not visited[next_pos]:
            if next_pos == n:
                print(d + 1)
                exit()
            visited[next_pos] = True
            q.append((next_pos, d + 1))

print(-1)
0