結果

問題 No.281 門松と魔法(1)
コンテスト
ユーザー norioc
提出日時 2025-11-09 03:19:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 66 ms / 1,000 ms
コード長 1,029 bytes
コンパイル時間 366 ms
コンパイル使用メモリ 82,188 KB
実行使用メモリ 53,844 KB
最終ジャッジ日時 2025-11-09 03:19:55
合計ジャッジ時間 4,549 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

def ceildiv(a: int, b: int) -> int:
    return (a + b - 1) // b


INF = 1 << 60
D = int(input())
H1 = int(input())
H2 = int(input())
H3 = int(input())


# a > b > c にするのに必要な操作回数
def f(a, b, c):
    if D == 0:
        if a > b > c >= 0:
            return 0
        return -1

    res = 0
    if a <= b:
        x = ceildiv(b-a, D)
        res += x
        b = max(0, b - x * D)
        if a == b:
            res += 1
            b -= D

    if b <= c:
        x = ceildiv(c-b, D)
        res += x
        c = max(0, c - x * D)
        if b == c:
            res += 1
            c -= D

    if a > b > c >= 0:
        return res
    return -1
    #print(f'>> {a=} {b=} {c=} {res=}')
    #return res


ans = INF
# 真ん中が一番低い、または一番高い
for a, b, c in [(H2, H1, H3), (H2, H3, H1), (H1, H3, H2), (H3, H1, H2)]:
    # print()
    # print(f'{a=} {b=} {c=}')
    res = f(a, b, c)

    if res == -1: continue
    ans = min(ans, res)

if ans == INF:
    print(-1)
else:
    print(ans)
0