結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー w0mbatw0mbat
提出日時 2021-07-09 21:51:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 202 ms / 2,000 ms
コード長 3,186 bytes
コンパイル時間 341 ms
コンパイル使用メモリ 87,052 KB
実行使用メモリ 83,564 KB
最終ジャッジ日時 2023-09-14 08:34:58
合計ジャッジ時間 4,680 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
82,048 KB
testcase_01 AC 92 ms
82,184 KB
testcase_02 AC 202 ms
83,300 KB
testcase_03 AC 195 ms
83,492 KB
testcase_04 AC 193 ms
83,404 KB
testcase_05 AC 183 ms
83,096 KB
testcase_06 AC 184 ms
83,564 KB
testcase_07 AC 181 ms
83,100 KB
testcase_08 AC 183 ms
83,440 KB
testcase_09 AC 181 ms
83,500 KB
testcase_10 AC 181 ms
83,300 KB
testcase_11 AC 159 ms
83,364 KB
testcase_12 AC 159 ms
83,488 KB
testcase_13 AC 157 ms
83,488 KB
testcase_14 AC 73 ms
71,208 KB
testcase_15 AC 74 ms
71,436 KB
testcase_16 AC 75 ms
71,208 KB
testcase_17 AC 73 ms
71,432 KB
testcase_18 AC 75 ms
70,996 KB
testcase_19 AC 76 ms
70,996 KB
権限があれば一括ダウンロードができます

ソースコード

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