結果

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

ソースコード

diff #

N = int(input())
def calc(n):
    s = bin(n)
    l = len(s)-2
    c = 0
    for i in range(l):
        if n>>i & 1 == 1:
            c += 1
    return c

copy = [-1 for _ in range(N)]
copy[0] = 1
used = [0 for _ in range(N)]
from collections import deque
que = deque()
que.append(1)
while que:
    i = que.popleft()
    if used[i-1] == 1:
        continue
    used[i-1] = 1
    n = calc(i)
    j = n
    if i - j > 0:
        if copy[i-j-1] == -1:
            copy[i-j-1] = copy[i-1]+1
            que.append(i-j)
    if i + j <= N : 
        if copy[i+j-1] == -1:
            copy[i+j-1] = copy[i-1]+1
            que.append(i+j)
print(copy[N-1])
0