結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
6,220 KB
testcase_01 AC 13 ms
6,208 KB
testcase_02 AC 13 ms
6,192 KB
testcase_03 AC 16 ms
6,428 KB
testcase_04 AC 14 ms
6,260 KB
testcase_05 AC 19 ms
6,348 KB
testcase_06 AC 16 ms
6,208 KB
testcase_07 AC 14 ms
6,304 KB
testcase_08 AC 18 ms
6,412 KB
testcase_09 AC 22 ms
6,408 KB
testcase_10 AC 25 ms
6,440 KB
testcase_11 AC 21 ms
6,420 KB
testcase_12 AC 19 ms
6,380 KB
testcase_13 AC 16 ms
6,496 KB
testcase_14 AC 23 ms
6,520 KB
testcase_15 AC 26 ms
6,512 KB
testcase_16 AC 24 ms
6,412 KB
testcase_17 AC 25 ms
6,624 KB
testcase_18 AC 15 ms
6,356 KB
testcase_19 AC 26 ms
6,428 KB
testcase_20 AC 13 ms
6,192 KB
testcase_21 AC 13 ms
6,416 KB
testcase_22 AC 24 ms
6,588 KB
testcase_23 AC 26 ms
6,568 KB
testcase_24 AC 25 ms
6,508 KB
testcase_25 AC 26 ms
6,636 KB
testcase_26 AC 13 ms
6,344 KB
testcase_27 AC 16 ms
6,312 KB
testcase_28 AC 24 ms
6,420 KB
testcase_29 AC 21 ms
6,548 KB
testcase_30 AC 13 ms
6,208 KB
testcase_31 AC 13 ms
6,408 KB
testcase_32 AC 19 ms
6,324 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