結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー nawawannawawan
提出日時 2021-07-09 21:51:37
言語 C++14
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 163 ms / 2,000 ms
コード長 2,925 bytes
コンパイル時間 1,368 ms
使用メモリ 13,148 KB
最終ジャッジ日時 2023-02-01 23:06:07
合計ジャッジ時間 4,749 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 14 ms
13,076 KB
testcase_01 AC 13 ms
13,136 KB
testcase_02 AC 160 ms
13,084 KB
testcase_03 AC 161 ms
13,036 KB
testcase_04 AC 163 ms
13,040 KB
testcase_05 AC 163 ms
12,992 KB
testcase_06 AC 162 ms
12,960 KB
testcase_07 AC 162 ms
12,980 KB
testcase_08 AC 163 ms
13,012 KB
testcase_09 AC 159 ms
13,076 KB
testcase_10 AC 163 ms
13,008 KB
testcase_11 AC 143 ms
13,072 KB
testcase_12 AC 143 ms
13,032 KB
testcase_13 AC 143 ms
13,052 KB
testcase_14 AC 14 ms
13,052 KB
testcase_15 AC 15 ms
13,092 KB
testcase_16 AC 14 ms
13,012 KB
testcase_17 AC 14 ms
13,148 KB
testcase_18 AC 15 ms
13,036 KB
testcase_19 AC 14 ms
12,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<const int MOD> struct modint{
    long long val;
    modint(): val(0) {}
    modint(long long x){
        if(x < 0) val = x % MOD + MOD;
        else val = x % MOD;
    }
    modint(const modint &t){
        val = t.val;
    }
    modint& operator =(const modint m){
        val = m.val;
        return *this;
    }
    modint operator -(){
        return modint(-val);
    }
    modint& operator-=(const modint &m){
        val -= m.val;
        if(val < 0) val += MOD;
        return *this;
    }
    modint& operator+=(const modint &m){
        val += m.val;
        if(val >= MOD) val -= MOD;
        return *this;
    }
    modint& operator*=(const modint &m){
        val *= m.val;
        val %= MOD;
        return *this;
    }
    modint& operator/=(modint m){
        *this *= m.inv();
        return *this;
    }
    modint inv(){
        long long x = 1, y = 0;
        long long a = val, b = MOD;
        while(b != 0){
            long long t = a / b;
            a -= t * b;
            x -= t * y;
            swap(a, b);
            swap(x, y);
        }
        x %= MOD;
        if(x < 0) x += MOD;
        return modint(x);
    }
    modint pow(long long k){
        long long res = 1;
        long long v = val;
        while(k > 0){
            if(k & 1) res = res * v % MOD;
            v = v * v % MOD;
            k >>= 1;
        }
        return modint(res);
    }
    modint operator==(const modint &m){
        return val == m.val;
    }
    modint operator+(const modint &m){
        return modint(*this) += m;
    }
    modint operator-(const modint &m){
        return modint(*this) -= m;
    }
    modint operator*(const modint &m){
        return modint(*this) *= m;
    }
    modint operator/(const modint &m){
        return modint(*this) /= m;
    }
    bool operator!=(const modint &m){
        return modint(*this).val != m.val;
    }
    bool operator!=(const int &m){
        return modint(*this).val != m;
    }
};
const int MOD = 1000000007;
using mint = modint<MOD>;
const int MAX = 410000;
mint fac[MAX], finv[MAX], inv[MAX];
//MAX < MOD
void COMinit(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i;
        inv[i] = mint(MOD) - inv[MOD % i] * (MOD / i);
        finv[i] = finv[i - 1] * inv[i];
    }
}
mint COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * finv[k] * finv[n - k];
}
int main(){
    int N, M;
    cin >> N >> M;
    COMinit();
    mint ans = COM(N + N, N) * 2 * N;
    for(int i = 0; i < M; i++){
        int t, x, y;
        cin >> t >> x >> y;
        if(t == 1){
            ans -= COM(x + y, x) * COM(N - (x + 1) + N - y, N - y);
        }
        else{
            ans -= COM(x + y, x) * COM(N - x + N - (y + 1), N - x);
        }
    }
    cout << ans.val << endl;
}
0