結果

問題 No.498 ワープクリスタル (給料日編)
ユーザー rlangevin
提出日時 2023-10-27 12:31:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 2,394 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 82,160 KB
実行使用メモリ 76,648 KB
最終ジャッジ日時 2024-09-25 12:59:32
合計ジャッジ時間 2,695 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

gx, gy, K = map(int, input().split())
X, Y, N = [0] * K, [0] * K, [0] * K
for i in range(K):
    X[i], Y[i], N[i] = map(int, input().split())

mod = 10 ** 9 + 7
n = 100
fact = [1] * (n + 1)
invfact = [1] * (n + 1)
for i in range(1, n):
    fact[i + 1] = ((i+1) * fact[i]) % mod
invfact[n] = pow(fact[n], mod - 2, mod)
for i in range(n - 1, -1, -1):
    invfact[i] = invfact[i + 1] * (i + 1) % mod

def comb(n, r):
    if n < 0 or r < 0 or n - r < 0:
        return 0
    return fact[n] * invfact[r] * invfact[n - r] % mod

ans = 0
if K == 1:
    for i0 in range(N[0] + 1):
        x = X[0] * i0
        y = Y[0] * i0
        if x == gx and y == gy:
            ans += 1
elif K == 2:
    for i0 in range(N[0] + 1):
        for i1 in range(N[1] + 1):
            x = X[0] * i0 + X[1] * i1
            y = Y[0] * i0 + Y[1] * i1
            if x == gx and y == gy:
                ans += comb(i0 + i1, i0)
                ans %= mod
elif K == 3:
    for i0 in range(N[0] + 1):
        for i1 in range(N[1] + 1):
            for i2 in range(N[2] + 1):
                x = X[0] * i0 + X[1] * i1 + X[2] * i2
                y = Y[0] * i0 + Y[1] * i1 + Y[2] * i2
                if x == gx and y == gy:
                    ans += comb(i0 + i1 + i2, i0) * comb(i1 + i2, i1)
                    ans %= mod
elif K == 4:
    for i0 in range(N[0] + 1):
        for i1 in range(N[1] + 1):
            for i2 in range(N[2] + 1):
                for i3 in range(N[3] + 1):
                    x = X[0] * i0 + X[1] * i1 + X[2] * i2 + X[3] * i3
                    y = Y[0] * i0 + Y[1] * i1 + Y[2] * i2 + Y[3] * i3
                    if x == gx and y == gy:
                        ans += comb(i0 + i1 + i2 + i3, i0) * comb(i1 + i2 + i3, i1) * comb(i2 + i3, i2)
                        ans %= mod
else:
    for i0 in range(N[0] + 1):
        for i1 in range(N[1] + 1):
            for i2 in range(N[2] + 1):
                for i3 in range(N[3] + 1):
                    for i4 in range(N[4] + 1):
                        x = X[0] * i0 + X[1] * i1 + X[2] * i2 + X[3] * i3 + X[4] * i4
                        y = Y[0] * i0 + Y[1] * i1 + Y[2] * i2 + Y[3] * i3 + Y[4] * i4
                        if x == gx and y == gy:
                            ans += comb(i0 + i1 + i2 + i3 + i4, i0) * comb(i1 + i2 + i3 + i4, i1) * comb(i2 + i3 + i4, i2) * comb(i3 + i4, i3)
                            ans %= mod

print(ans)
0