結果

問題 No.3 ビットすごろく
ユーザー nbisconbisco
提出日時 2016-04-09 23:12:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 32 ms / 5,000 ms
コード長 662 bytes
コンパイル時間 118 ms
コンパイル使用メモリ 10,784 KB
実行使用メモリ 8,672 KB
最終ジャッジ日時 2023-09-13 23:49:11
合計ジャッジ時間 2,340 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,524 KB
testcase_01 AC 19 ms
8,552 KB
testcase_02 AC 19 ms
8,552 KB
testcase_03 AC 23 ms
8,496 KB
testcase_04 AC 20 ms
8,576 KB
testcase_05 AC 25 ms
8,608 KB
testcase_06 AC 22 ms
8,412 KB
testcase_07 AC 22 ms
8,556 KB
testcase_08 AC 24 ms
8,576 KB
testcase_09 AC 28 ms
8,572 KB
testcase_10 AC 29 ms
8,480 KB
testcase_11 AC 28 ms
8,524 KB
testcase_12 AC 26 ms
8,568 KB
testcase_13 AC 21 ms
8,540 KB
testcase_14 AC 29 ms
8,672 KB
testcase_15 AC 32 ms
8,524 KB
testcase_16 AC 30 ms
8,448 KB
testcase_17 AC 30 ms
8,512 KB
testcase_18 AC 21 ms
8,540 KB
testcase_19 AC 31 ms
8,544 KB
testcase_20 AC 19 ms
8,488 KB
testcase_21 AC 18 ms
8,428 KB
testcase_22 AC 29 ms
8,584 KB
testcase_23 AC 32 ms
8,640 KB
testcase_24 AC 31 ms
8,544 KB
testcase_25 AC 31 ms
8,592 KB
testcase_26 AC 19 ms
8,640 KB
testcase_27 AC 22 ms
8,616 KB
testcase_28 AC 30 ms
8,652 KB
testcase_29 AC 26 ms
8,568 KB
testcase_30 AC 19 ms
8,492 KB
testcase_31 AC 19 ms
8,532 KB
testcase_32 AC 27 ms
8,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/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