結果
| 問題 | No.1596 Distance Sum in 2D Plane | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-07-09 22:00:55 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 291 ms / 2,000 ms | 
| コード長 | 1,332 bytes | 
| コンパイル時間 | 1,632 ms | 
| コンパイル使用メモリ | 171,436 KB | 
| 実行使用メモリ | 11,208 KB | 
| 最終ジャッジ日時 | 2024-07-01 16:22:53 | 
| 合計ジャッジ時間 | 6,207 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 17 | 
ソースコード
#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = (l); i < (r); i++)
using namespace std;
typedef long long ll;
ll mod_pow(ll x, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}
int main() {
    ll N, M, MOD = 1000000007;
    cin >> N >> M;
    vector<ll> t(M), x(M), y(M), f(2 * N + 1, 1);
    rep(i, 0, 2 * N) f[i + 1] = (f[i] * (i + 1)) % MOD;
    ll ans = f[2 * N] * mod_pow(f[N], MOD - 2, MOD);
    ans %= MOD;
    ans *= mod_pow(f[N], MOD - 2, MOD);
    ans %= MOD;
    ans *= 2 * N;
    ans %= MOD;
    rep(i, 0, M) cin >> t[i] >> x[i] >> y[i];
    rep(i, 0, M) {
        ll d = f[x[i] + y[i]] * mod_pow(f[x[i]], MOD - 2, MOD);
        d %= MOD;
        d *= mod_pow(f[y[i]], MOD - 2, MOD);
        d %= MOD;
        d *= f[2 * N - x[i] - y[i] - 1];
        d %= MOD;
        if (t[i] == 1) {
            d *= mod_pow(f[N - x[i] - 1], MOD - 2, MOD);
            d %= MOD;
            d *= mod_pow(f[N - y[i]], MOD - 2, MOD);
            d %= MOD;
        } else {
            d *= mod_pow(f[N - x[i]], MOD - 2, MOD);
            d %= MOD;
            d *= mod_pow(f[N - y[i] - 1], MOD - 2, MOD);
            d %= MOD;
        }
        ans += MOD - d;
        ans %= MOD;
    }
    cout << ans << endl;
}
            
            
            
        