結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー ransewhale
提出日時 2021-07-10 00:47:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 247 ms / 2,000 ms
コード長 1,081 bytes
コンパイル時間 1,326 ms
コンパイル使用メモリ 76,960 KB
最終ジャッジ日時 2025-01-22 23:15:36
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

using ll = long long int;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = (ll)(1e9+7);

#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	a = (a<MOD)?a:(a-MOD);
	b = (b<MOD)?b:(b-MOD);
	a += b;
	a = (a<MOD)?a:(a-MOD);
}

ll fc[410410]={1ll},nv[410410]={1ll};

ll rwpw(ll a, ll p = MOD-2){
	ll ret = 1ll;
	while(p){
		if(p%2){
			mul_mod(ret,a);
		}
		mul_mod(a,a);
		p /= 2;
	}
	return ret;
}

ll c(int n, int k){
	ll ret = fc[n];
	mul_mod(ret,nv[k]);
	mul_mod(ret,nv[n-k]);
	return ret;
}

int main(void){
	int n,i,m,t,x,y;
	ll ans,tmp;
	for(i=1; i<410410; ++i){
		fc[i] = fc[i-1]*i%MOD;
		nv[i] = rwpw(fc[i]);
	}
	std::cin >> n >> m;
	ans = c(n*2,n);
	mul_mod(ans,n*2);
	for(i=0; i<m; ++i){
		std::cin >> t >> x >> y; --t;
		tmp = c(x+y,x);
		if(t){
			++y;
		}else{
			++x;
		}
		x = n-x; y = n-y;
		mul_mod(tmp,c(x+y,y));
		add_mod(ans,MOD-tmp);
	}
	std::cout << ans << std::endl;
	return 0;
}
0