結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー MZKiMZKi
提出日時 2021-07-09 22:14:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 2,745 bytes
コンパイル時間 1,194 ms
コンパイル使用メモリ 146,312 KB
実行使用メモリ 20,172 KB
最終ジャッジ日時 2023-09-14 09:27:18
合計ジャッジ時間 5,587 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
19,992 KB
testcase_01 AC 30 ms
19,980 KB
testcase_02 AC 196 ms
19,868 KB
testcase_03 AC 192 ms
19,984 KB
testcase_04 AC 193 ms
19,980 KB
testcase_05 AC 193 ms
20,172 KB
testcase_06 AC 192 ms
20,040 KB
testcase_07 AC 193 ms
19,860 KB
testcase_08 AC 191 ms
20,024 KB
testcase_09 AC 197 ms
19,984 KB
testcase_10 AC 195 ms
19,872 KB
testcase_11 AC 169 ms
20,056 KB
testcase_12 AC 169 ms
20,172 KB
testcase_13 AC 168 ms
20,044 KB
testcase_14 AC 30 ms
20,148 KB
testcase_15 AC 29 ms
19,864 KB
testcase_16 AC 30 ms
19,988 KB
testcase_17 AC 30 ms
19,872 KB
testcase_18 AC 32 ms
19,856 KB
testcase_19 AC 31 ms
19,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define eps (double)(1e-9)
#define pi (double) acos(-1)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

struct mint {
	ll x;
	mint(ll x = 0) :x((x%mod + mod) % mod) {}
	mint operator-() const { return mint(-x); }
	mint& operator+=(const mint a) {
		if ((x += a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator-=(const mint a) {
		if ((x += mod - a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator*=(const mint a) {
		(x *= a.x) %= mod;
		return *this;
	}
	mint operator+(const mint a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint a) const {
		mint res(*this);
		return res *= a;
	}
	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}
	mint inv() const {
		return pow(mod - 2);
	}
	mint& operator/=(const mint a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint a) const {
		mint res(*this);
		return res /= a;
	}
	bool operator==(const mint a) const {
		return x == a.x;
	}
	bool operator!=(const mint a) const {
		return x != a.x;
	}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }


const int MAX = 710000;
long long fac[MAX], finv[MAX], inv[MAX];

//使う前に実行
//pが素数かつp>nが条件
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 % mod;
        inv[i] = mod - inv[mod%i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
}
 
long long 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] % mod) % mod;
}

int main(){
    int n, m;
    cin >> n >> m;
    COMinit();
    mint ans = COM(2*n, n);
    ans *= 2*n;
    rep(i, m){
        int t, x, y;
        cin >> t >> x >> y;
        if(t == 1){
            mint now = 1;
            now *= COM(x+y, x);
            now *= COM(2*n-x-y-1, n-y);
            ans -= now;
        }else{
            mint now = 1;
            now *= COM(x+y, x);
            now *= COM(2*n-x-y-1, n-x);
            ans -= now;
        }
    }
    cout << ans << endl;
}
0