結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー e869120e869120
提出日時 2021-07-09 21:16:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,024 bytes
コンパイル時間 740 ms
コンパイル使用メモリ 83,448 KB
実行使用メモリ 38,668 KB
最終ジャッジ日時 2023-09-14 07:03:26
合計ジャッジ時間 20,307 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 808 ms
25,852 KB
testcase_02 AC 1,168 ms
38,656 KB
testcase_03 AC 1,156 ms
38,424 KB
testcase_04 AC 1,159 ms
38,428 KB
testcase_05 AC 1,163 ms
38,488 KB
testcase_06 AC 1,156 ms
38,376 KB
testcase_07 AC 1,155 ms
38,352 KB
testcase_08 AC 1,172 ms
38,472 KB
testcase_09 AC 1,161 ms
38,668 KB
testcase_10 AC 1,155 ms
38,372 KB
testcase_11 AC 1,022 ms
38,396 KB
testcase_12 AC 1,024 ms
38,352 KB
testcase_13 AC 1,021 ms
38,464 KB
testcase_14 RE -
testcase_15 AC 806 ms
25,848 KB
testcase_16 AC 804 ms
25,844 KB
testcase_17 AC 803 ms
25,900 KB
testcase_18 RE -
testcase_19 AC 808 ms
25,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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(1 <= 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;
}
0