結果

問題 No.2287 ++ -- *=a /=a
ユーザー 👑 rin204rin204
提出日時 2024-04-29 22:21:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,443 ms / 2,000 ms
コード長 1,288 bytes
コンパイル時間 238 ms
コンパイル使用メモリ 82,276 KB
実行使用メモリ 97,572 KB
最終ジャッジ日時 2024-04-29 22:22:06
合計ジャッジ時間 15,363 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
52,864 KB
testcase_01 AC 64 ms
65,792 KB
testcase_02 AC 65 ms
66,304 KB
testcase_03 AC 60 ms
64,640 KB
testcase_04 AC 62 ms
65,664 KB
testcase_05 AC 74 ms
66,944 KB
testcase_06 AC 486 ms
79,836 KB
testcase_07 AC 465 ms
79,524 KB
testcase_08 AC 487 ms
79,416 KB
testcase_09 AC 332 ms
78,240 KB
testcase_10 AC 337 ms
78,300 KB
testcase_11 AC 293 ms
78,212 KB
testcase_12 AC 306 ms
77,896 KB
testcase_13 AC 329 ms
78,336 KB
testcase_14 AC 308 ms
78,700 KB
testcase_15 AC 282 ms
78,132 KB
testcase_16 AC 267 ms
78,284 KB
testcase_17 AC 307 ms
78,336 KB
testcase_18 AC 1,270 ms
87,312 KB
testcase_19 AC 914 ms
82,488 KB
testcase_20 AC 883 ms
81,904 KB
testcase_21 AC 792 ms
81,136 KB
testcase_22 AC 699 ms
81,160 KB
testcase_23 AC 731 ms
81,076 KB
testcase_24 AC 672 ms
80,988 KB
testcase_25 AC 694 ms
81,260 KB
testcase_26 AC 647 ms
80,268 KB
testcase_27 AC 1,443 ms
97,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import *


def solve():
    x, y, a = map(int, input().split())
    E = []
    X = [x, y]
    s = x
    t = y
    while x > 0:
        nx = x // a
        X.append(nx)
        E.append((x, nx, 1))
        x = nx

    while y > 0:
        ny = y // a
        E.append((ny, ny * a, 1))
        E.append((ny + 1, (ny + 1) * a, 1))
        E.append((ny, ny + 1, 1))
        E.append((ny + 1, ny, 1))
        X.append(ny)
        X.append(ny * a)
        X.append(ny + 1)
        X.append((ny + 1) * a)
        y = ny

    X_ = sorted(list(set(X)))
    X = {x: i for i, x in enumerate(X_)}
    le = len(X)
    edges = [[] for _ in range(le)]
    for u, v, c in E:
        edges[X[u]].append((X[v], c))

    for i in range(le - 1):
        d = X_[i + 1] - X_[i]
        edges[i].append((i + 1, d))
        edges[i + 1].append((i, d))

    dist = [1 << 60] * le
    s = X[s]
    t = X[t]
    dist[s] = 0
    hq = [s]
    while hq:
        tmp = heappop(hq)
        d = tmp // le
        pos = tmp - le * d
        if dist[pos] < d:
            continue
        for npos, c in edges[pos]:
            nd = d + c
            if nd < dist[npos]:
                dist[npos] = nd
                heappush(hq, nd * le + npos)

    print(dist[t])


for _ in range(int(input())):
    solve()
0