結果

問題 No.3 ビットすごろく
ユーザー 141sksk
提出日時 2019-06-24 23:37:50
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
RE  
実行時間 -
コード長 512 bytes
コンパイル時間 479 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-06-22 15:16:40
合計ジャッジ時間 1,915 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 1 RE * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

goal = int(input())
    
def bin1(n):
    return bin(n).count("1")
    
Q = [0] * (goal + 1)
q = [1]
Q[1] = 1
    
while q:
    position = q.pop()
    stepLength = bin1(position)
    stepCount = Q[position]
    go = position + stepLength
    back = position - stepLength
    if go < goal + 1 and Q[go] == 0:
        Q[go] = stepCount + 1
        q.append(go)
    elif back > 0 and Q[go] == 0:
        Q[back] = stepCount + 1
        q.append(back)
        
if Q[goal] != 0:
    print(Q[goal])
else:
    print(-1)
0