結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー w0mbatw0mbat
提出日時 2021-07-09 22:17:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 3,186 bytes
コンパイル時間 449 ms
コンパイル使用メモリ 87,196 KB
実行使用メモリ 83,840 KB
最終ジャッジ日時 2023-09-14 09:33:11
合計ジャッジ時間 4,131 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
82,436 KB
testcase_01 AC 80 ms
82,344 KB
testcase_02 AC 166 ms
83,752 KB
testcase_03 AC 160 ms
83,744 KB
testcase_04 AC 160 ms
83,772 KB
testcase_05 AC 154 ms
83,680 KB
testcase_06 AC 159 ms
83,376 KB
testcase_07 AC 163 ms
83,568 KB
testcase_08 AC 154 ms
83,528 KB
testcase_09 AC 154 ms
83,656 KB
testcase_10 AC 152 ms
83,596 KB
testcase_11 AC 142 ms
83,800 KB
testcase_12 AC 150 ms
83,840 KB
testcase_13 AC 146 ms
83,772 KB
testcase_14 AC 67 ms
71,412 KB
testcase_15 AC 69 ms
71,292 KB
testcase_16 AC 69 ms
71,364 KB
testcase_17 AC 68 ms
71,296 KB
testcase_18 AC 67 ms
71,284 KB
testcase_19 AC 67 ms
71,364 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