結果

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
using ll = long long;
constexpr int MAX = 5e5;
constexpr ll mod = 1e9+7;

struct mint {
  ll x;
  mint(ll _x = 0) {if((_x %= mod) < 0) _x += mod; x = _x;}
  bool operator==(const mint a) {return x == a.x;}
  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;
  }
  // for prime mod
  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;
  }
  ll val() {return x;}
};

mint fac[MAX], finv[MAX];

void combinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  for (int i = 2; i < MAX; i++){
    fac[i] = fac[i-1] * mint(i);
    finv[i] = finv[i-1] * mint(i).inv();
  }
}

mint comb(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;
  combinit();
  mint ans = comb(2*n,n)*2*n;
  while(m--) {
    int t, x, y;
    cin >> t >> x >> y;
    if(t == 1) ans -= comb(x+y,x)*comb(2*n-x-y-1,n-y);
    if(t == 2) ans -= comb(x+y,x)*comb(2*n-x-y-1,n-x);
  }
  cout << ans.val() << endl;
  return 0;
}
0