結果
| 問題 |
No.1596 Distance Sum in 2D Plane
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-20 18:59:47 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 234 ms / 2,000 ms |
| コード長 | 1,471 bytes |
| コンパイル時間 | 235 ms |
| コンパイル使用メモリ | 82,772 KB |
| 実行使用メモリ | 135,936 KB |
| 最終ジャッジ日時 | 2025-03-20 19:00:27 |
| 合計ジャッジ時間 | 4,912 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 |
ソースコード
MOD = 10**9 + 7
def main():
import sys
input = sys.stdin.read().split()
ptr = 0
N = int(input[ptr])
ptr += 1
M = int(input[ptr])
ptr += 1
max_fac = 400000 + 10 # Precompute up to 4e5 +10 to handle all cases
fac = [1] * (max_fac + 1)
for i in range(1, max_fac + 1):
fac[i] = fac[i-1] * i % MOD
inv_fac = [1] * (max_fac + 1)
inv_fac[max_fac] = pow(fac[max_fac], MOD - 2, MOD)
for i in range(max_fac - 1, -1, -1):
inv_fac[i] = inv_fac[i + 1] * (i + 1) % MOD
def comb(a, b):
if a < 0 or b < 0 or a < b:
return 0
return fac[a] * inv_fac[b] % MOD * inv_fac[a - b] % MOD
total_paths = comb(2 * N, N)
total_cost = total_paths * 2 * N % MOD
sum_free = 0
for _ in range(M):
t = int(input[ptr])
ptr += 1
x = int(input[ptr])
ptr += 1
y = int(input[ptr])
ptr += 1
if t == 1:
a = x + y
way1 = comb(a, x)
rem_r = N - (x + 1)
rem_c = N - y
c = rem_r + rem_c
way2 = comb(c, rem_r)
else:
a = x + y
way1 = comb(a, x)
rem_r = N - x
rem_c = N - (y + 1)
c = rem_r + rem_c
way2 = comb(c, rem_r)
sum_free = (sum_free + way1 * way2) % MOD
ans = (total_cost - sum_free) % MOD
print(ans)
if __name__ == "__main__":
main()
lam6er