結果
| 問題 | No.1596 Distance Sum in 2D Plane | 
| コンテスト | |
| ユーザー |  Tomii9273 | 
| 提出日時 | 2021-07-10 04:30:40 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 295 ms / 2,000 ms | 
| コード長 | 1,530 bytes | 
| コンパイル時間 | 166 ms | 
| コンパイル使用メモリ | 82,944 KB | 
| 実行使用メモリ | 135,552 KB | 
| 最終ジャッジ日時 | 2024-07-01 23:57:40 | 
| 合計ジャッジ時間 | 5,453 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 17 | 
ソースコード
import sys
# sys.setrecursionlimit(10 ** 9)  # Codeforcesでは350000程度に
def input(): return sys.stdin.readline().strip()
n, m = map(int, input().split())
A = [list(map(int, input().split())) for i in range(m)]
mod = 10**9 + 7
le = 4*10**5 + 3
def pow(x, y):  # x**y の mod を返す。modは素数でなくてもよい。
    x %= mod
    ans = 1
    while y > 0:
        if y % 2 == 1:
            ans = (ans * x) % mod
        x = (x**2) % mod
        y //= 2
    return ans % mod
def inv(x):  # x の mod での逆元を返す。modが素数で、xとmodが互いに素である必要あり。
    x %= mod
    if x == 2:
        return (mod + 1) // 2
    return pow(x, mod - 2)
M = [1]  # i!
mul = 1
for i in range(1, le):
    mul = (mul * i) % mod
    M.append(mul)
MI = [0] * (le-1) + [inv(M[le-1])]  # i!の逆元
for i in range(le-2, -1, -1):
    MI[i] = MI[i+1] * (i+1) % mod
def C(x, y):  # コンビネーション (組合せ, 二項係数)
    if y < 0 or y > x:
        return 0
    elif x > le:  # O(min(y, x-y))
        y = min(y, x-y)
        ans = 1
        for i in range(x, x-y, -1):
            ans = (ans * i) % mod
        return (ans * MI[y]) % mod
    else:  # O(1)
        ans = M[x]
        ans = (ans * MI[y]) % mod
        return (ans * MI[x-y]) % mod
ans = C(2*n, n) * 2 * n
for i in range(m):
    t, x, y = tuple(A[i])
    if t == 1:
        ans -= C(x+y, x) * C(n-(x+1) + n-y, n-(x+1))
    else:
        ans -= C(x+y, x) * C(n-x + n-(y+1), n-x)
    ans %= mod
print(ans % mod)
            
            
            
        