結果

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

ソースコード

diff #

import collections
N = int(input())
lscost = [float('INF')]*(N+1)
used = [False]*(N+1)
lscost[1] = 1
d = collections.deque()
d.append(1)
while d:
    v = d.popleft()
    if used[v]:
        continue
    used[v] = True
    bitlen = bin(v).count('1')
    g = [v-bitlen,v+bitlen]
    for j in g:
        if j > N:
            continue
        if lscost[j] > lscost[v]+1:
            lscost[j] = lscost[v]+1
            d.append(j)
if lscost[N] == float('INF'):
    print(-1)
else:
    print(lscost[N])
0