結果
問題 | No.1596 Distance Sum in 2D Plane |
ユーザー |
![]() |
提出日時 | 2021-07-09 21:50:46 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 549 ms / 2,000 ms |
コード長 | 1,699 bytes |
コンパイル時間 | 2,589 ms |
コンパイル使用メモリ | 77,060 KB |
実行使用メモリ | 52,100 KB |
最終ジャッジ日時 | 2024-07-01 15:56:25 |
合計ジャッジ時間 | 10,836 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 |
ソースコード
import java.io.BufferedReader;import java.io.InputStreamReader;import java.math.BigInteger;public class Main {public static void main(String[] args) throws Exception {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String[] sa = br.readLine().split(" ");int n = Integer.parseInt(sa[0]);int m = Integer.parseInt(sa[1]);int[] t = new int[m];int[] x = new int[m];int[] y = new int[m];for (int i = 0; i < m; i++) {sa = br.readLine().split(" ");t[i] = Integer.parseInt(sa[0]);x[i] = Integer.parseInt(sa[1]);y[i] = Integer.parseInt(sa[2]);}br.close();int mod = 1000000007;Kaijou kai = new Kaijou(n * 2, mod);long ans = kai.comb(n * 2, n) * n * 2 % mod;for (int i = 0; i < m; i++) {long v1 = kai.comb(x[i] + y[i], x[i]);long v2 = 0;if (t[i] == 1) {v2 = kai.comb(n * 2 - x[i] - y[i] - 1, n - x[i] - 1);} else {v2 = kai.comb(n * 2 - x[i] - y[i] - 1, n - x[i]);}long val = v1 * v2 % mod;ans = ans - val + mod;ans %= mod;}System.out.println(ans);}static class Kaijou {long[] p, pi;int m;public Kaijou(int n, int mod) {n++;m = mod;p = new long[n];pi = new long[n];p[0] = 1;pi[0] = 1;for (int i = 1; i < n; i++) {p[i] = p[i - 1] * i % m;}pi[n - 1] = BigInteger.valueOf(p[n - 1]).modInverse(BigInteger.valueOf(m)).longValue();for (int i = n - 1; i > 1; i--) {pi[i - 1] = pi[i] * i % m;}}public long comb(int n, int r) {if (n < r) return 0;return p[n] * pi[r] % m * pi[n - r] % m;}public long perm(int n, int r) {if (n < r) return 0;return p[n] * pi[n - r] % m;}}}