結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー kenskens
提出日時 2021-07-10 01:09:02
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 272 ms / 2,000 ms
コード長 1,846 bytes
コンパイル時間 2,095 ms
コンパイル使用メモリ 201,028 KB
実行使用メモリ 11,420 KB
最終ジャッジ日時 2023-09-14 12:37:06
合計ジャッジ時間 7,709 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
11,192 KB
testcase_01 AC 109 ms
11,316 KB
testcase_02 AC 270 ms
11,120 KB
testcase_03 AC 270 ms
11,112 KB
testcase_04 AC 272 ms
11,420 KB
testcase_05 AC 272 ms
11,172 KB
testcase_06 AC 271 ms
11,124 KB
testcase_07 AC 271 ms
11,124 KB
testcase_08 AC 271 ms
11,212 KB
testcase_09 AC 269 ms
11,100 KB
testcase_10 AC 271 ms
11,216 KB
testcase_11 AC 245 ms
11,304 KB
testcase_12 AC 243 ms
11,156 KB
testcase_13 AC 243 ms
11,100 KB
testcase_14 AC 109 ms
11,172 KB
testcase_15 AC 109 ms
11,128 KB
testcase_16 AC 109 ms
11,148 KB
testcase_17 AC 110 ms
11,176 KB
testcase_18 AC 110 ms
11,228 KB
testcase_19 AC 110 ms
11,164 KB
権限があれば一括ダウンロードができます

ソースコード

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