結果

問題 No.281 門松と魔法(1)
ユーザー kimiyuki
提出日時 2016-06-14 18:14:52
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 38 ms / 1,000 ms
コード長 1,175 bytes
コンパイル時間 129 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-11-06 20:46:39
合計ジャッジ時間 3,527 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
d = int(input())
h1 = int(input())
h2 = int(input())
h3 = int(input())
def is_kadomatsu(a, b, c):
    assert a >= 0 and b >= 0 and c >= 0
    return a != c and ((a < b and b > c) or (a > b and b < c))
def make_lt(a, b):
    if b == 0:
        return float('inf')
    else:
        return max(0, (a - (b-1) + d-1) // d)
def check_count(k1, k2, k3):
    if k1 < 0 or k2 < 0 or k3 < 0:
        return float('inf')
    g1 = max(0, h1 - k1 * d)
    g2 = max(0, h2 - k2 * d)
    g3 = max(0, h3 - k3 * d)
    if is_kadomatsu(g1, g2, g3):
        return k1 + k2 + k3
    else:
        return float('inf')
if d == 0:
    if is_kadomatsu(h1, h2, h3):
        ans = 0
    else:
        ans = -1
else:
    ans = float('inf')
    k1 = make_lt(h1, h2)
    k2 = make_lt(h2, min(h1, h3))
    k3 = make_lt(h3, h2)
    for i1 in range(-3,3+1):
        for i2 in range(-3,3+1):
            for i3 in range(-3,3+1):
                for l1 in [0, k1]:
                    for l2 in [0, k2]:
                        for l3 in [0, k3]:
                            ans = min(ans, check_count(l1 + i1, l2 + i2, l3 + i3))
    if ans == float('inf'):
        ans = -1
print(ans)
0