結果

問題 No.3 ビットすごろく
ユーザー DialBirdDialBird
提出日時 2017-03-12 07:20:33
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 23 ms / 5,000 ms
コード長 733 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 10,992 KB
実行使用メモリ 8,596 KB
最終ジャッジ日時 2023-09-14 00:34:36
合計ジャッジ時間 2,061 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,308 KB
testcase_01 AC 16 ms
8,276 KB
testcase_02 AC 16 ms
8,208 KB
testcase_03 AC 17 ms
8,280 KB
testcase_04 AC 16 ms
8,212 KB
testcase_05 AC 20 ms
8,472 KB
testcase_06 AC 17 ms
8,244 KB
testcase_07 AC 16 ms
8,256 KB
testcase_08 AC 18 ms
8,464 KB
testcase_09 AC 20 ms
8,424 KB
testcase_10 AC 21 ms
8,524 KB
testcase_11 AC 19 ms
8,408 KB
testcase_12 AC 18 ms
8,344 KB
testcase_13 AC 15 ms
8,264 KB
testcase_14 AC 20 ms
8,548 KB
testcase_15 AC 23 ms
8,560 KB
testcase_16 AC 20 ms
8,596 KB
testcase_17 AC 23 ms
8,384 KB
testcase_18 AC 16 ms
8,352 KB
testcase_19 AC 23 ms
8,528 KB
testcase_20 AC 15 ms
8,188 KB
testcase_21 AC 15 ms
8,204 KB
testcase_22 AC 21 ms
8,424 KB
testcase_23 AC 23 ms
8,424 KB
testcase_24 AC 23 ms
8,420 KB
testcase_25 AC 22 ms
8,444 KB
testcase_26 AC 15 ms
8,336 KB
testcase_27 AC 16 ms
8,372 KB
testcase_28 AC 21 ms
8,528 KB
testcase_29 AC 19 ms
8,476 KB
testcase_30 AC 15 ms
8,212 KB
testcase_31 AC 14 ms
8,180 KB
testcase_32 AC 19 ms
8,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Yukicoder:
    def __init__(self):
        self.n = int(input())

    def bit_count(self, n):
        res = 0
        while n > 0:
            n &= n - 1
            res += 1
        return res

    def run(self):
        dp = [0 for i in range(10001)]
        dp[1] = 1

        queue = [1]
        while len(queue) != 0:
            q = queue.pop(0)
            if q == self.n:
                return dp[q]
            cnt = self.bit_count(q)
            for i in [-1, 1]:
                next_pos = q + cnt*i
                if 0 < next_pos and next_pos <= self.n and dp[next_pos] == 0:
                    dp[next_pos] = dp[q] + 1
                    queue.append(next_pos)
        return -1

y = Yukicoder()
print(y.run())
0