結果
問題 | No.1596 Distance Sum in 2D Plane |
ユーザー |
|
提出日時 | 2021-11-15 11:19:40 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 218 ms / 2,000 ms |
コード長 | 1,365 bytes |
コンパイル時間 | 2,233 ms |
コンパイル使用メモリ | 183,444 KB |
実行使用メモリ | 12,896 KB |
最終ジャッジ日時 | 2024-12-14 09:34:06 |
合計ジャッジ時間 | 6,758 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 |
ソースコード
#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; }