結果

問題 No.3 ビットすごろく
ユーザー kichirb3kichirb3
提出日時 2018-03-07 19:47:18
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 41 ms / 5,000 ms
コード長 1,886 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 10,976 KB
実行使用メモリ 12,308 KB
最終ジャッジ日時 2023-09-14 00:56:39
合計ジャッジ時間 2,487 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,896 KB
testcase_01 AC 21 ms
8,808 KB
testcase_02 AC 21 ms
8,808 KB
testcase_03 AC 25 ms
9,668 KB
testcase_04 AC 23 ms
9,040 KB
testcase_05 AC 32 ms
10,688 KB
testcase_06 AC 26 ms
9,624 KB
testcase_07 AC 25 ms
9,336 KB
testcase_08 AC 29 ms
10,196 KB
testcase_09 AC 34 ms
11,256 KB
testcase_10 AC 41 ms
11,640 KB
testcase_11 AC 32 ms
10,944 KB
testcase_12 AC 31 ms
10,584 KB
testcase_13 AC 26 ms
9,440 KB
testcase_14 AC 36 ms
11,612 KB
testcase_15 AC 39 ms
12,148 KB
testcase_16 AC 38 ms
12,096 KB
testcase_17 AC 39 ms
12,276 KB
testcase_18 AC 25 ms
9,408 KB
testcase_19 AC 39 ms
12,236 KB
testcase_20 AC 23 ms
9,056 KB
testcase_21 AC 21 ms
8,708 KB
testcase_22 AC 36 ms
11,500 KB
testcase_23 AC 39 ms
12,240 KB
testcase_24 AC 38 ms
12,308 KB
testcase_25 AC 39 ms
12,280 KB
testcase_26 AC 21 ms
8,824 KB
testcase_27 AC 25 ms
9,640 KB
testcase_28 AC 38 ms
11,940 KB
testcase_29 AC 32 ms
11,104 KB
testcase_30 AC 21 ms
8,884 KB
testcase_31 AC 23 ms
8,780 KB
testcase_32 AC 32 ms
10,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-
"""
No.3 ビットすごろく
https://yukicoder.me/problems/no/3

"""
import sys
from sys import stdin
from functools import lru_cache
from heapq import heappop, heappush
input = stdin.readline


@lru_cache(maxsize=None)
def pop_count7(i):
    ans = 0
    while i:
        ans += 1
        i &= (i - 1)
    return ans

def pop_count(i):
    upper = i >> 7
    lower = i % 128
    ans = pop_count7(upper) + pop_count7(lower)
    return ans


def dijkstra(adj, s):
    # ダイクストラ法で始点からの距離を計算する
    WHITE, GRAY, BLACK = 0, 1, 2
    color = [WHITE] * len(adj)
    d = [float('inf')] * len(adj)    # 始点からの距離 計算結果
    d[s] = 0                    #  始点から自身までの距離は0
    pq = []
    heappush(pq, (0, s))
    while pq:
        cost, u = heappop(pq)
        color[u] = BLACK
        if d[u] < cost:
            continue
        for v, cost in adj[u]:
            if color[v] == BLACK:
                continue
            if d[v] > d[u] + cost:
                d[v] = d[u] + cost
                heappush(pq, (d[v], v))
                color[v] = GRAY
    return d                    #  計算した距離情報を返す


def main(args):
    N = int(input())

    adj = [[] for _ in range(N+1)] #  隣接リスト
    for i in range(1, N+1):     #  1〜Nそれぞれについて、直接到達できるマスを調べてコスト1の有向グラフにする
        b = pop_count(i)
        if 1<= i-b <= N:
            adj[i].append([i-b, 1])
        if 1<= i+b <= N:
            adj[i].append([i+b, 1])

    dist = dijkstra(adj, 1)     #  1からの最短移動数を計算

    if dist[-1] != float('inf'):
        print(dist[-1] + 1)     #  開始のマスも移動数にカウントするので+1する
    else:
        print(-1)


if __name__ == '__main__':
    main(sys.argv[1:])
0