結果

問題 No.1596 Distance Sum in 2D Plane
コンテスト
ユーザー Rumain831
提出日時 2026-09-05 09:39:00
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 123 ms / 2,000 ms
+ 646µs
コード長 974 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,464 ms
コンパイル使用メモリ 179,848 KB
実行使用メモリ 15,972 KB
最終ジャッジ日時 2026-09-05 09:39:36
合計ジャッジ時間 6,638 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
using ll = long long;

ll modpow(ll a, ll b, ll p){
  a%=p;
  ll ans=1;
  while(b>0){
    if(b%2==1) ans=(ans*a)%p;
    b/=2;
    a=(a*a)%p;
  }
  return ans;
}

pair<vector<ll>, vector<ll>> factorial(int n, ll mod){
  vector<ll> fact(n+1, 1), invfact(n+1, 1);
  for(ll i=2; i<=n; i++) fact[i]=fact[i-1]*i%mod;
  invfact[n]=modpow(fact[n], mod-2, mod);
  for(ll i=n-1; i>=1; i--) invfact[i]=invfact[i+1]*(i+1)%mod;
  return {fact, invfact};
}

int main(void){
  int n, m; cin >> n >> m;
  ll mod=1e9+7;
  auto [fact, inv]=factorial(2*n+1, mod);
  auto nCk=[&](int n, int k){
    return fact[n]*inv[k]%mod*inv[n-k]%mod;
  };
  ll ans=nCk(2*n, n)*2*n%mod;
  while(m--){
    int t, x, y; cin >> t >> x >> y;
    ll minus, dx, dy;
    if(t==1) dx=n-x-1, dy=n-y;
    else dx=n-x, dy=n-y-1;
    minus=nCk(x+y, x)*nCk(dx+dy, dx)%mod;
    ans=(ans-minus+mod)%mod;
  }
  cout << ans << endl;
  return 0; 
}
0