結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー w0mbatw0mbat
提出日時 2021-07-09 21:51:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 3,186 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 82,032 KB
実行使用メモリ 82,572 KB
最終ジャッジ日時 2024-07-01 15:57:12
合計ジャッジ時間 3,259 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
MOD = 10**9 + 7
def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    fu = FactorialUtils(N + N)
    ans = Mint(fu.choose(N + N, N))
    ans *= N + N
    for _ in range(M):
        t, x, y = map(int, input().split())
        if t == 1:
            ans -= fu.choose(x + y, x) * fu.choose(N + N - x - y - 1, N - y)
        else:
            ans -= fu.choose(x + y, x) * fu.choose(N + N - x - y - 1, N - x)
    print(ans)

class Mint:
    __slots__ = ('value')

    def __init__(self, value=0) -> None:
        self.value = int(value) % MOD

    def inverse(self) -> int:
        a, b = self.value, MOD
        u, v = 1, 0
        while b:
            t = a // b
            b, a = a - t * b, b
            v, u = u - t * v, v
        if u < 0: u += MOD
        return u

    def __repr__(self) -> str: return str(self.value)
    def __int__(self) -> int: return self.value
    def __eq__(self, other) -> bool: return self.value == other.value
    def __neg__(self) -> 'Mint': return Mint(-self.value)
    def __hash__(self) -> int: return hash(self.value)
    def __bool__(self) -> bool: return self.value != 0

    def __iadd__(self, other) -> 'Mint':
        self.value = (self.value + int(other)) % MOD
        return self

    def __add__(self, other) -> 'Mint':
        new_obj = Mint(self.value + int(other))
        return new_obj
    __radd__ = __add__

    def __isub__(self, other) -> 'Mint':
        self.value = (self.value - int(other)) % MOD
        return self

    def __sub__(self, other) -> 'Mint':
        new_obj = Mint(self.value - int(other))
        return new_obj

    def __rsub__(self, other) -> 'Mint':
        new_obj = Mint(int(other) - self.value)
        return new_obj

    def __imul__(self, other) -> 'Mint':
        self.value = self.value * int(other) % MOD
        return self

    def __mul__(self, other) -> 'Mint':
        new_obj = Mint(self.value * int(other))
        return new_obj
    __rmul__ = __mul__

    def __ifloordiv__(self, other) -> 'Mint':
        other = other if isinstance(other, Mint) else Mint(other)
        self *= other.inverse()
        return self

    def __floordiv__(self, other) -> 'Mint':
        new_obj = Mint(self.value)
        new_obj //= other
        return new_obj

    def __rfloordiv__(self, other) -> 'Mint':
        new_obj = Mint(int(other))
        new_obj //= self
        return new_obj


class FactorialUtils:
    __slots__ = ('fac', 'ifac')

    def __init__(self, n):
        self.fac = [1] * (n + 1)
        self.ifac = [1] * (n + 1)
        for i in range(2, n + 1): self.fac[i] = self.fac[i - 1] * i % MOD
        self.ifac[n] = pow(self.fac[n], MOD - 2, MOD)
        for i in range(n, 1, -1): self.ifac[i - 1] = self.ifac[i] * i % MOD

    def choose(self, n, r):
        if r < 0 or r > n: return 0
        return (self.fac[n] * self.ifac[n - r] % MOD) * self.ifac[r] % MOD

    def multichoose(self, u, k):
        return (self.fac[u + k - 1] * self.ifac[u - 1] % MOD) * self.ifac[k] % MOD

    def permutation(self, n, r):
        if r < 0 or r > n: return 0
        return self.fac[n] * self.ifac[n - r] % MOD

if __name__ == '__main__':
    main()
0