結果
| 問題 |
No.1596 Distance Sum in 2D Plane
|
| コンテスト | |
| ユーザー |
Tomii9273
|
| 提出日時 | 2021-07-10 04:28:26 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,530 bytes |
| コンパイル時間 | 283 ms |
| コンパイル使用メモリ | 82,560 KB |
| 実行使用メモリ | 130,304 KB |
| 最終ジャッジ日時 | 2024-07-01 23:54:40 |
| 合計ジャッジ時間 | 4,241 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 3 |
| other | AC * 2 TLE * 1 -- * 14 |
ソースコード
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 = 3*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)
Tomii9273