結果

問題 No.3 ビットすごろく
コンテスト
ユーザー nbisco
提出日時 2016-04-09 23:12:44
言語 Python3
(3.14.3 + numpy 2.4.2 + scipy 1.17.0)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 108 ms / 5,000 ms
コード長 662 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 675 ms
コンパイル使用メモリ 20,952 KB
実行使用メモリ 15,484 KB
最終ジャッジ日時 2026-03-22 04:06:07
合計ジャッジ時間 5,004 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#!/usr/bin/env python3
#fileencoding: utf-8

from collections import deque

def popcnt(i):
    count = 0
    while i > 0:
        count += (i&0x1)
        i >>= 1
    return count

def bfs(N):
    visited = [0] * N
    queue = deque()
    queue.append((1,1))  # (index, score)
    while queue:
        p = queue.popleft()
        if visited[p[0]-1] == 1:
            continue
        visited[p[0]-1] = 1
        if p[0] == N:
            return p[1]
        bits = popcnt(p[0])
        if p[0]+bits <= N:
            queue.append((p[0]+bits, p[1]+1))
        if p[0]-bits > 0:
            queue.append((p[0]-bits,p[1]+1))
    return -1

print(bfs(int(input())))
0