結果
問題 | No.1596 Distance Sum in 2D Plane |
ユーザー |
![]() |
提出日時 | 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 + 7def main():import sysinput = sys.stdin.read().split()ptr = 0N = int(input[ptr])ptr += 1M = int(input[ptr])ptr += 1max_fac = 400000 + 10 # Precompute up to 4e5 +10 to handle all casesfac = [1] * (max_fac + 1)for i in range(1, max_fac + 1):fac[i] = fac[i-1] * i % MODinv_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) % MODdef comb(a, b):if a < 0 or b < 0 or a < b:return 0return fac[a] * inv_fac[b] % MOD * inv_fac[a - b] % MODtotal_paths = comb(2 * N, N)total_cost = total_paths * 2 * N % MODsum_free = 0for _ in range(M):t = int(input[ptr])ptr += 1x = int(input[ptr])ptr += 1y = int(input[ptr])ptr += 1if t == 1:a = x + yway1 = comb(a, x)rem_r = N - (x + 1)rem_c = N - yc = rem_r + rem_cway2 = comb(c, rem_r)else:a = x + yway1 = comb(a, x)rem_r = N - xrem_c = N - (y + 1)c = rem_r + rem_cway2 = comb(c, rem_r)sum_free = (sum_free + way1 * way2) % MODans = (total_cost - sum_free) % MODprint(ans)if __name__ == "__main__":main()