結果

問題 No.3068 Speedrun (Hard)
ユーザー norioc
提出日時 2025-03-22 08:14:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,668 ms / 2,000 ms
コード長 1,047 bytes
コンパイル時間 363 ms
コンパイル使用メモリ 82,392 KB
実行使用メモリ 76,072 KB
最終ジャッジ日時 2025-03-22 08:14:25
合計ジャッジ時間 7,515 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

A, B, C, D, N = map(int, input().split())
P, Q, R, S, T = map(int, input().split())


def is_valid(a, b, c, d) -> bool:
    if not (0 <= a <= A): return False
    if not (0 <= b <= B): return False
    if not (0 <= c <= C): return False
    if not (0 <= d <= D): return False

    return a+b+c+d == N and a*P + b*Q + c*R + d*S == T


def solve():
    for a in range(A+1):
        if a+B+C+D < N: continue
        for b in range(B+1):
            n = N - a - b
            t = T - a*P - b*Q

            if S == R:
                if n*R != t: continue
                # c と d は同じ問題とみなせる
                c = min(C, n)
                d = n - c
                if is_valid(a, b, c, d):
                    return a, b, c, d
            else:
                if (t - n*R) % (S - R) != 0: continue
                d = (t - n*R) // (S - R)
                c = n - d
                if is_valid(a, b, c, d):
                    return a, b, c, d

    return None


ans = solve()
if ans is None:
    print(-1)
else:
    print(*ans)
0