結果

問題 No.3 ビットすごろく
ユーザー UekiUeki
提出日時 2019-11-06 16:20:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 32 ms / 5,000 ms
コード長 621 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 10,772 KB
実行使用メモリ 8,612 KB
最終ジャッジ日時 2023-09-14 01:29:39
合計ジャッジ時間 2,062 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,976 KB
testcase_01 AC 16 ms
8,028 KB
testcase_02 AC 16 ms
8,048 KB
testcase_03 AC 20 ms
8,264 KB
testcase_04 AC 18 ms
8,036 KB
testcase_05 AC 24 ms
8,324 KB
testcase_06 AC 20 ms
8,060 KB
testcase_07 AC 17 ms
7,976 KB
testcase_08 AC 23 ms
8,284 KB
testcase_09 AC 26 ms
8,572 KB
testcase_10 AC 28 ms
8,508 KB
testcase_11 AC 26 ms
8,484 KB
testcase_12 AC 24 ms
8,308 KB
testcase_13 AC 19 ms
8,160 KB
testcase_14 AC 28 ms
8,404 KB
testcase_15 AC 31 ms
8,464 KB
testcase_16 AC 31 ms
8,448 KB
testcase_17 AC 32 ms
8,596 KB
testcase_18 AC 19 ms
7,952 KB
testcase_19 AC 31 ms
8,520 KB
testcase_20 AC 16 ms
8,152 KB
testcase_21 AC 14 ms
8,028 KB
testcase_22 AC 27 ms
8,492 KB
testcase_23 AC 29 ms
8,604 KB
testcase_24 AC 30 ms
8,612 KB
testcase_25 AC 31 ms
8,520 KB
testcase_26 AC 16 ms
8,048 KB
testcase_27 AC 18 ms
8,028 KB
testcase_28 AC 30 ms
8,436 KB
testcase_29 AC 26 ms
8,412 KB
testcase_30 AC 15 ms
8,016 KB
testcase_31 AC 16 ms
7,972 KB
testcase_32 AC 24 ms
8,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N = int(input())
dp = [None]*(N+1)  # dp[i]: マスIにたどり着ける最小の手数
dp[1] = 0


def popcount(n):
    return bin(n).count("1")


Q = [[1, 1]]
while Q:
    n, p = heapq.heappop(Q)
    pop = popcount(p)
    if 1 <= p+pop <= N:
        if dp[p+pop] is None:
            dp[p+pop] = dp[p]+1
            heapq.heappush(Q, [n+1, p+pop])
    if 1 <= p-pop <= N:
        if dp[p-pop] is None:
            dp[p-pop] = dp[p]+1
            heapq.heappush(Q, [n+1, p-pop])
if dp[-1] is None:
    print(-1)
else:
    print(dp[-1]+1)
0