結果

問題 No.3 ビットすごろく
ユーザー kichirb3kichirb3
提出日時 2018-03-07 20:04:55
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 36 ms / 5,000 ms
コード長 1,877 bytes
コンパイル時間 82 ms
コンパイル使用メモリ 11,096 KB
実行使用メモリ 12,352 KB
最終ジャッジ日時 2023-09-14 00:56:50
合計ジャッジ時間 2,237 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,760 KB
testcase_01 AC 19 ms
8,760 KB
testcase_02 AC 19 ms
8,752 KB
testcase_03 AC 25 ms
9,608 KB
testcase_04 AC 23 ms
8,920 KB
testcase_05 AC 30 ms
10,608 KB
testcase_06 AC 25 ms
9,716 KB
testcase_07 AC 24 ms
9,256 KB
testcase_08 AC 27 ms
10,212 KB
testcase_09 AC 31 ms
11,324 KB
testcase_10 AC 33 ms
11,640 KB
testcase_11 AC 30 ms
11,140 KB
testcase_12 AC 29 ms
10,712 KB
testcase_13 AC 26 ms
9,544 KB
testcase_14 AC 33 ms
11,512 KB
testcase_15 AC 36 ms
12,096 KB
testcase_16 AC 36 ms
12,072 KB
testcase_17 AC 35 ms
12,240 KB
testcase_18 AC 24 ms
9,296 KB
testcase_19 AC 36 ms
12,352 KB
testcase_20 AC 20 ms
8,952 KB
testcase_21 AC 19 ms
8,816 KB
testcase_22 AC 32 ms
11,584 KB
testcase_23 AC 34 ms
12,272 KB
testcase_24 AC 36 ms
12,304 KB
testcase_25 AC 36 ms
12,132 KB
testcase_26 AC 20 ms
8,848 KB
testcase_27 AC 25 ms
9,580 KB
testcase_28 AC 35 ms
12,028 KB
testcase_29 AC 32 ms
10,964 KB
testcase_30 AC 21 ms
8,772 KB
testcase_31 AC 20 ms
8,928 KB
testcase_32 AC 31 ms
10,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-
"""
No.3 ビットすごろく
https://yukicoder.me/problems/no/3
AC
"""
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〜Nそれぞれについて、直接到達できるマスを調べてコスト1の有向グラフにする
        b = pop_count(i)
        if 1<= i-b:
            adj[i].append([i-b, 1])
        if 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