結果

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

ソースコード

diff #

n = int(input())

a = [float("inf") for _ in range(n * 2 + 10)]
a[1] = 1

b = [0 for _ in range(n * 2 + 10)]
b[0] = 1

s = []
s_1 = [1,1]
s.append(s_1)
while len(s) != 0 and b[n] != 1:
    s.sort()
    t = s.pop(0)
    u = str(bin(t[1])).count("1")
    b[t[1]] = 1
    if t[1] < n:
        if b[t[1] + u] != 1 and a[t[1]] + 1 < a[t[1] + u]:
            a[t[1] + u] = a[t[1]] + 1
            s_1 = [a[t[1]] + 1 , t[1] + u]
            s.append(s_1)
        if b[t[1] - u] != 1 and a[t[1]] + 1 < a[t[1] - u]:
            a[t[1] - u] = a[t[1]] + 1
            s_1 = [a[t[1]] + 1 , t[1] - u]
            s.append(s_1)


if a[n] == float("inf"):
    print(-1)
else:
    print(a[n])
0