結果

問題 No.3 ビットすごろく
ユーザー yuki2006yuki2006
提出日時 2014-10-02 20:14:03
言語 Python2
(2.7.18)
結果
AC  
実行時間 26 ms / 5,000 ms
コード長 694 bytes
コンパイル時間 867 ms
コンパイル使用メモリ 6,692 KB
実行使用メモリ 6,696 KB
最終ジャッジ日時 2023-09-13 22:50:59
合計ジャッジ時間 2,708 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
6,284 KB
testcase_01 AC 13 ms
6,228 KB
testcase_02 AC 13 ms
6,288 KB
testcase_03 AC 16 ms
6,492 KB
testcase_04 AC 14 ms
6,284 KB
testcase_05 AC 19 ms
6,292 KB
testcase_06 AC 16 ms
6,260 KB
testcase_07 AC 14 ms
6,200 KB
testcase_08 AC 18 ms
6,416 KB
testcase_09 AC 22 ms
6,492 KB
testcase_10 AC 23 ms
6,500 KB
testcase_11 AC 21 ms
6,320 KB
testcase_12 AC 19 ms
6,392 KB
testcase_13 AC 15 ms
6,276 KB
testcase_14 AC 23 ms
6,360 KB
testcase_15 AC 26 ms
6,564 KB
testcase_16 AC 25 ms
6,548 KB
testcase_17 AC 26 ms
6,484 KB
testcase_18 AC 14 ms
6,192 KB
testcase_19 AC 26 ms
6,416 KB
testcase_20 AC 13 ms
6,248 KB
testcase_21 AC 13 ms
6,260 KB
testcase_22 AC 24 ms
6,464 KB
testcase_23 AC 25 ms
6,696 KB
testcase_24 AC 26 ms
6,428 KB
testcase_25 AC 25 ms
6,544 KB
testcase_26 AC 13 ms
6,320 KB
testcase_27 AC 15 ms
6,300 KB
testcase_28 AC 24 ms
6,456 KB
testcase_29 AC 21 ms
6,600 KB
testcase_30 AC 13 ms
6,188 KB
testcase_31 AC 13 ms
6,200 KB
testcase_32 AC 20 ms
6,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def bitCount(n):
    count = 0
    while n > 0:
        n = n & (n - 1)
        count += 1
    return count


def bfs(N):
    table = [0] * (N + 1)

    q = deque()
    q.append(1)
    table[1] = 1

    while len(q) > 0:
        current = q.popleft()
        if current == N:
            return table[current]
        a = current + bitCount(current)
        b = current - bitCount(current)

        if 1 <= a <= N and table[a] == 0:
            q.append(a)
            table[a] = table[current] + 1

        if 1 <= b <= N and table[b] == 0:
            q.append(b)
            table[b] = table[current] + 1
    return -1


N = int(raw_input())

print (bfs(N))

0