結果

問題 No.281 門松と魔法(1)
ユーザー mban
提出日時 2017-07-26 08:29:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 39 ms / 1,000 ms
コード長 1,363 bytes
コンパイル時間 320 ms
コンパイル使用メモリ 82,440 KB
実行使用メモリ 54,108 KB
最終ジャッジ日時 2024-10-09 17:34:53
合計ジャッジ時間 3,779 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

d = int(input())
H1 = int(input())
H2 = int(input())
H3 = int(input())

impossible = 9999999999

ans = impossible


def Kadomatsu(a, b, c):
    if a == b or b == c or c == a:
        return False
    if a <= b and b <= c:
        return False
    if a >= b and b >= c:
        return False
    return True


def A(a, b, c):
    result = 0
    if a == c:
        if a <= d:
            return impossible
        else:
            a = max(0, a - d)
            result += 1
    m = min(a, c)

    if b >= m:
        sa = b - m
        bb = sa // d + 1
        b = max(0, b - bb * d)
        result += bb
    if Kadomatsu(a, b, c):
        return result
    else:
        return impossible


def B(a, b, c):
    result = 0
    if b == 0:
        return impossible
    if a >= b:
        sa = a - b
        aa = sa // d + 1
        result += aa
        a = max(0, a - aa * d)
    if c >= b:
        sa = c - b
        cc = sa // d + 1
        result += cc
        c = max(0, c - cc * d)
    if a == c:
        if a == 0:
            return impossible
        result += 1
        a = max(0, a - d)
    if Kadomatsu(a, b, c):
        return result
    else:
        return impossible


if Kadomatsu(H1, H2, H3):
    print(0)
else:
    if d == 0:
        print(-1)
    else:
        ans = min(A(H1, H2, H3), B(H1, H2, H3))
        print(-1 if ans == impossible else ans)
0