結果
問題 | No.1596 Distance Sum in 2D Plane |
ユーザー | e869120 |
提出日時 | 2021-07-09 21:17:03 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,168 ms / 2,000 ms |
コード長 | 2,024 bytes |
コンパイル時間 | 816 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 36,480 KB |
最終ジャッジ日時 | 2024-07-01 14:31:55 |
合計ジャッジ時間 | 22,927 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 810 ms
19,072 KB |
testcase_01 | AC | 815 ms
18,944 KB |
testcase_02 | AC | 1,164 ms
36,352 KB |
testcase_03 | AC | 1,168 ms
36,224 KB |
testcase_04 | AC | 1,166 ms
36,224 KB |
testcase_05 | AC | 1,161 ms
36,224 KB |
testcase_06 | AC | 1,158 ms
36,224 KB |
testcase_07 | AC | 1,157 ms
36,224 KB |
testcase_08 | AC | 1,160 ms
36,480 KB |
testcase_09 | AC | 1,156 ms
36,224 KB |
testcase_10 | AC | 1,153 ms
36,224 KB |
testcase_11 | AC | 1,040 ms
36,480 KB |
testcase_12 | AC | 1,039 ms
36,224 KB |
testcase_13 | AC | 1,039 ms
36,352 KB |
testcase_14 | AC | 811 ms
19,072 KB |
testcase_15 | AC | 810 ms
19,072 KB |
testcase_16 | AC | 809 ms
19,072 KB |
testcase_17 | AC | 811 ms
19,072 KB |
testcase_18 | AC | 811 ms
18,944 KB |
testcase_19 | AC | 810 ms
19,072 KB |
ソースコード
#include <iostream> #include <cassert> #include <map> #include <tuple> using namespace std; long long modpow(long long a, long long b, long long m) { long long p = 1, q = a; for (int i = 0; i < 30; i++) { if ((b / (1LL << i)) % 2LL == 1) { p *= q; p %= m; } q *= q; q %= m; } return p; } long long Div(long long a, long long b, long long m) { return (a * modpow(b, m - 2, m)) % m; } long long mod = 1000000007; long long N, M; long long A[1 << 18], B[1 << 18], C[1 << 18]; long long fact[1 << 20], factinv[1 << 20]; map<tuple<int, int, int>, int> Map; long long ncr(long long n, long long r) { return (fact[n] * factinv[r] % mod) * factinv[n - r] % mod; } long long keiro(long long a, long long b) { // (0, 0) から (a, b) まで移動する方法の総数 return ncr(a + b, b); } int main() { // Step #1. Input cin >> N >> M; for (int i = 1; i <= M; i++) cin >> A[i] >> B[i] >> C[i]; assert(1 <= N && N <= 200000); assert(0 <= M && M <= 200000); for (int i = 1; i <= M; i++) { assert(1 <= A[i] && A[i] <= 2); assert(Map[make_tuple(A[i], B[i], C[i])] == 0); Map[make_tuple(A[i], B[i], C[i])] = 1; if (A[i] == 1) { assert(0 <= B[i] && B[i] <= N - 1); assert(0 <= C[i] && C[i] <= N); } if (A[i] == 2) { assert(0 <= B[i] && B[i] <= N); assert(0 <= C[i] && C[i] <= N - 1); } } // Step #2. Prepare fact[0] = 1; for (int i = 1; i <= 1000000; i++) fact[i] = (1LL * i * fact[i - 1]) % mod; for (int i = 0; i <= 1000000; i++) factinv[i] = Div(1, fact[i], mod); // Step #3. Solve long long Answer = (2LL * N) * keiro(N, N) % mod; for (int i = 1; i <= M; i++) { if (A[i] == 1) { long long p1 = keiro(B[i], C[i]); long long p2 = keiro(N - (B[i] + 1LL), N - C[i]); Answer -= p1 * p2 % mod; Answer = (Answer + mod) % mod; } if (A[i] == 2) { long long p1 = keiro(B[i], C[i]); long long p2 = keiro(N - B[i], N - (C[i] + 1LL)); Answer -= p1 * p2 % mod; Answer = (Answer + mod) % mod; } } // Step #4. Output cout << Answer << endl; return 0; }