結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
6,420 KB
testcase_01 AC 14 ms
6,276 KB
testcase_02 AC 14 ms
6,200 KB
testcase_03 AC 17 ms
6,236 KB
testcase_04 AC 14 ms
6,308 KB
testcase_05 AC 19 ms
6,312 KB
testcase_06 AC 16 ms
6,308 KB
testcase_07 AC 14 ms
6,420 KB
testcase_08 AC 18 ms
6,488 KB
testcase_09 AC 21 ms
6,348 KB
testcase_10 AC 23 ms
6,428 KB
testcase_11 AC 21 ms
6,344 KB
testcase_12 AC 20 ms
6,296 KB
testcase_13 AC 15 ms
6,464 KB
testcase_14 AC 23 ms
6,488 KB
testcase_15 AC 26 ms
6,440 KB
testcase_16 AC 24 ms
6,416 KB
testcase_17 AC 26 ms
6,504 KB
testcase_18 AC 15 ms
6,256 KB
testcase_19 AC 26 ms
6,496 KB
testcase_20 AC 13 ms
6,208 KB
testcase_21 AC 13 ms
6,200 KB
testcase_22 AC 23 ms
6,440 KB
testcase_23 AC 26 ms
6,416 KB
testcase_24 AC 26 ms
6,440 KB
testcase_25 AC 26 ms
6,492 KB
testcase_26 AC 12 ms
6,352 KB
testcase_27 AC 15 ms
6,432 KB
testcase_28 AC 24 ms
6,648 KB
testcase_29 AC 21 ms
6,348 KB
testcase_30 AC 13 ms
6,248 KB
testcase_31 AC 13 ms
6,280 KB
testcase_32 AC 20 ms
6,320 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