結果

問題 No.1683 Robot Guidance
ユーザー hitonanode
提出日時 2021-08-07 23:14:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 231 ms / 2,000 ms
コード長 1,632 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 121,856 KB
最終ジャッジ日時 2024-06-29 19:21:58
合計ジャッジ時間 6,130 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

class facmod:
    def __init__(self, N: int, md: int):
        assert N < md
        self.md = md
        self.facs = [1] * (N + 1)
        self.facinvs = [1] * (N + 1)
        for i in range(1, N + 1):
            self.facs[i] = self.facs[i - 1] * i % md
        self.facinvs[N] = pow(self.facs[i], md - 2, md)
        for i in range(N - 1, 0, -1):
            self.facinvs[i] = self.facinvs[i + 1] * (i + 1) % md

    def nCr(self, n: int, r: int) -> int:
        if n < 0 or r < 0 or n < r:
            return 0
        return (self.facs[n] * self.facinvs[r]) % md * self.facinvs[n - r] % md

md = 1000000007

A, B, X, Y = map(int, input().split())
fac = facmod(A + B + abs(X) + abs(Y), md)

ret = 0
nxplus = (B + 4) // 4
nyplus = (B + 3) // 4
nxminus = (B + 2) // 4
nyminus = (B + 1) // 4

for xplus in range(A + 1):
    xminus = xplus - X
    rem = A - xplus - xminus
    yplus = (rem + Y) // 2
    yminus = (rem - Y) // 2
    if xminus < 0 or yplus < 0 or yminus < 0 or yplus - yminus != Y:
        continue
    if xplus + xminus + yplus + yminus != A:
        continue
    if nyplus == 0 and yplus > 0:
        continue
    if nxminus == 0 and xminus > 0:
        continue
    if nyminus == 0 and yminus > 0:
        continue
    tmp = 1
    if xplus:
        tmp *= fac.nCr(nxplus - 1 + xplus, xplus)
    if yplus:
        tmp = tmp * fac.nCr(nyplus - 1 + yplus, yplus) % md
    if xminus:
        tmp = tmp * fac.nCr(nxminus - 1 + xminus, xminus) % md
    if yminus:
        tmp = tmp * fac.nCr(nyminus - 1 + yminus, yminus) % md
    
    ret += tmp

print(ret % md)
0