結果

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

ソースコード

diff #

from collections import deque

N = int(input())

def one_counter(num):
    return sum([ int(i) for i in bin(num)[2:] ])

line = [ one_counter(i) for i in range(1,N+1) ]
reach = [ -1 for i in range(N) ]

q = deque([0])
end = N - 1
reach[0] = 1

while q:
    pos = q.popleft()
    
    if pos == end:
        print(reach[-1])
        exit()
    
    next_move = [ line[pos], -line[pos] ]
    
    for m in next_move:
        next_pos = pos + m
        
        if not 0 <= next_pos <= end:
            continue
        
        if reach[next_pos] == -1:
            q.append(next_pos)
            reach[next_pos] = reach[pos] + 1
print(-1)
0