結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー qqq xxaqqq xxa
提出日時 2021-07-18 18:03:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 3,528 bytes
コンパイル時間 1,816 ms
コンパイル使用メモリ 171,236 KB
実行使用メモリ 17,408 KB
最終ジャッジ日時 2023-09-22 20:49:19
合計ジャッジ時間 5,640 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
12,524 KB
testcase_01 AC 16 ms
12,356 KB
testcase_02 AC 166 ms
17,280 KB
testcase_03 AC 166 ms
17,376 KB
testcase_04 AC 165 ms
17,052 KB
testcase_05 AC 167 ms
17,268 KB
testcase_06 AC 165 ms
17,104 KB
testcase_07 AC 166 ms
17,408 KB
testcase_08 AC 167 ms
17,216 KB
testcase_09 AC 165 ms
17,116 KB
testcase_10 AC 166 ms
17,204 KB
testcase_11 AC 156 ms
17,364 KB
testcase_12 AC 157 ms
17,388 KB
testcase_13 AC 156 ms
17,272 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const ll MOD = 1e9 + 7;
template<ll MOD> struct ModInt {
    ll value;
    ModInt() : value(0) {}
    ModInt(const ll& v) : value(v % MOD) {
        if ( value < 0 ) value += MOD;
    }
    ModInt<MOD>& operator=(const ModInt& o) {
        value = o.value;
        return *this;
    }
    inline bool operator==(const ModInt& o) {
        return value == o.value;
    }
    inline bool operator!=(const ModInt& o) {
        return value != o.value;
    }
    ModInt<MOD>& operator+=(const ModInt& o) {
        value += o.value;
        if ( value >= MOD ) value -= MOD;
        return *this;
    }
    ModInt<MOD>& operator-=(const ModInt& o) {
        value += MOD - o.value;
        if (value >= MOD ) value -= MOD;
        return *this;
    }
    ModInt<MOD>& operator*=(const ModInt& o) {
        value *= o.value;
        value %= MOD;
        return *this;
    }
    ModInt<MOD>& operator/=(const ModInt& o) {
        value *= inverse(o.value);
        value %= MOD;
        return *this;
    }
    ModInt<MOD> operator-() {
        return ModInt<MOD>(-value);
    }
    ModInt<MOD> operator+(const ModInt& o) {
        ModInt<MOD> r(*this);
        r += o;
        return r;
    }
    ModInt<MOD> operator-(const ModInt& o) {
        ModInt<MOD> r(*this);
        r -= o;
        return r;
    }
    ModInt<MOD> operator*(const ModInt& o) {
        ModInt<MOD> r(*this);
        r *= o;
        return r;
    }
    ModInt<MOD> operator/(const ModInt& o) {
        ModInt<MOD> r(*this);
        r /= o;
        return r;
    }
    ModInt<MOD> pow(ll n) {
        ModInt<MOD> t(*this), r = 1;
        while ( n > 0 ) {
            if ( n & 1 ) r *= t;
            t *= t;
            n >>= 1;
        }
        return r;
    }
private:
    ll inverse(ll a) {
        ModInt<MOD> t(a);
        return t.pow(MOD - 2).value;
    }
};
template<ll MOD> std::ostream& operator<< (std::ostream& os, const ModInt<MOD>& o) {
    os << o.value;
    return os;
}
template<ll MOD> std::istream& operator>> (std::istream& is, ModInt<MOD>& o) {
    is >> o.value;
    return is;
}

//---------------------------
// 二項係数
template<typename T> struct Combination {
    ll n_max;
    vector<T> fac, finv, inv;
    Combination(ll n_max) {
        fac.resize(n_max+1);
        finv.resize(n_max+1);
        inv.resize(n_max+1);
        fac[0] = fac[1] = 1;
        finv[0] = finv[1] = 1;
        inv[0] = 0, inv[1] = 1;
        for (int i = 2; i <= n_max; i++){
            fac[i] = fac[i - 1] * i;
            inv[i] = - inv[MOD%i] * (MOD / i);
            finv[i] = finv[i - 1] * inv[i];
        }
    }
    T C(ll n, ll k) {
        if (n < k) return 0;
        if (n < 0 || k < 0) return 0;
        return fac[n] * finv[k] * finv[n - k];
    }
    T P(ll n, ll k) {
        if (n < k) return 0;
        if (n < 0 || k < 0) return 0;
        return fac[n] * finv[n - k];
    }
    T H(ll n, ll k) {
        return C(n+k-1, k);
    }
};

using Int = ModInt<MOD>;


ll solve() {
    ll N, M;
    cin >> N >> M;
    vector<ll> T(M), X(M), Y(M);
    for ( int i = 0; i < M; i++ ) {
        cin >> T[i] >> X[i] >> Y[i];
    }
    
    Combination<Int> cb(N*2+10);
    Int ans = Int(2*N) * cb.C(2*N,N);
    for ( int i = 0; i < M; i++ ) {
        ll nx = X[i];
        if ( T[i] == 1 ) nx++;
        ans -= cb.C(X[i]+Y[i],X[i]) * cb.C(2*N-(X[i]+Y[i]+1),N-nx);
    }

    return ans.value;
}

int main() {
    auto ans = solve();
    cout << ans << "\n";
    return 0;
}
0