結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー simkarensimkaren
提出日時 2021-11-15 11:19:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 2,000 ms
コード長 1,365 bytes
コンパイル時間 2,637 ms
コンパイル使用メモリ 180,092 KB
実行使用メモリ 15,840 KB
最終ジャッジ日時 2023-08-21 01:54:11
合計ジャッジ時間 7,895 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
12,436 KB
testcase_01 AC 23 ms
13,184 KB
testcase_02 AC 178 ms
14,340 KB
testcase_03 AC 178 ms
12,864 KB
testcase_04 AC 176 ms
12,532 KB
testcase_05 AC 176 ms
14,492 KB
testcase_06 AC 175 ms
13,324 KB
testcase_07 AC 179 ms
15,840 KB
testcase_08 AC 173 ms
15,644 KB
testcase_09 AC 175 ms
13,256 KB
testcase_10 AC 176 ms
12,940 KB
testcase_11 AC 163 ms
12,736 KB
testcase_12 AC 163 ms
14,812 KB
testcase_13 AC 162 ms
12,688 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr ll mod = 1000000007LL;

ll modpow(ll a, ll b) {
	if (a > mod) a %= mod;
	if (b == 0LL) return 1LL;
	ll tmp = modpow(a, b / 2);
	if (b & 1LL)
		return tmp * tmp % mod * a % mod;
	return tmp * tmp % mod;
}

ll inverse(ll a) {
	return modpow(a, mod - 2);
}

ll fact(ll k, bool inverse = false) {
	static vector<ll> fac(2, 1LL);
	static vector<ll> inv(2, 1LL);
	static vector<ll> finv(2, 1LL);
	static ll nx = 2LL;
	while (nx <= k) {
		fac.push_back(fac[nx - 1] * nx % mod);
		inv.push_back(mod - inv[mod % nx] * (mod / nx) % mod);
		finv.push_back(finv[nx - 1] * inv[nx] % mod);
		++nx;
	}
	if (!inverse) return fac[k];
	return finv[k];
}

ll comb(ll a, ll b) {
	if (b < 0 || b > a) return 0;
	return fact(a) * fact(b, 1) % mod * fact(a - b, 1) % mod;
}

int main(void){
    int N, M;
    cin >> N >> M;
    ll ans = 2LL * N * comb(2 * N, N) % mod;
    for (int _ = 0; _ < M; ++_){
        int t, x, y;
        cin >> t >> x >> y;
        ll sub;
        if (t == 1){
            sub = comb(x + y, x) * comb(2 * N - x - y - 1, N - y) % mod;
        } else {
            sub = comb(x + y, x) * comb(2 * N - x - y - 1, N - x) % mod;
        }
        ans -= sub;
        if (ans < 0) ans += mod;
    }
    cout << ans << endl;
    return 0;
}
0