結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー kumakumaaaaakumakumaaaaa
提出日時 2021-07-09 21:59:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 1,585 bytes
コンパイル時間 1,538 ms
コンパイル使用メモリ 167,668 KB
実行使用メモリ 15,068 KB
最終ジャッジ日時 2023-09-14 08:59:58
合計ジャッジ時間 4,107 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
14,792 KB
testcase_01 AC 25 ms
14,996 KB
testcase_02 AC 90 ms
14,756 KB
testcase_03 AC 91 ms
14,896 KB
testcase_04 AC 88 ms
14,760 KB
testcase_05 AC 88 ms
15,044 KB
testcase_06 AC 89 ms
14,940 KB
testcase_07 AC 89 ms
14,764 KB
testcase_08 AC 88 ms
14,860 KB
testcase_09 AC 88 ms
14,964 KB
testcase_10 AC 89 ms
14,796 KB
testcase_11 AC 73 ms
15,048 KB
testcase_12 AC 74 ms
15,048 KB
testcase_13 AC 73 ms
15,068 KB
testcase_14 AC 25 ms
14,904 KB
testcase_15 AC 24 ms
14,848 KB
testcase_16 AC 24 ms
15,036 KB
testcase_17 AC 25 ms
14,896 KB
testcase_18 AC 25 ms
14,812 KB
testcase_19 AC 23 ms
14,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// #include <atcoder/modint>
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define INF 2000000000000000000
#define ll long long
#define ld long double
#define pll pair<ll, ll>
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

const ll MAX = 510010;
const ll mod = 1000000007;
vector<ll> fac(MAX), finv(MAX), inv(MAX);
bool init_called = false;
//=============modcom============================
void COMinit() {
  init_called = true;
  fac.at(0) = 1;
  fac.at(1) = 1;
  finv.at(0) = 1;
  finv.at(1) = 1;
  inv.at(1) = 1;
  for (ll i = 2; i < MAX; i++){
    fac.at(i) = fac.at(i - 1) * i % mod;
    inv.at(i) = mod - inv.at(mod % i) * (mod / i) % mod;
    finv.at(i) = finv.at(i - 1) * inv.at(i) % mod;
  }
}

ll COM(ll n, ll k){
  if (!init_called) {
    COMinit();
  }
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac.at(n) * (finv.at(k) * finv.at(n - k) % mod) % mod;
}
//=================================================

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, M;
  cin >> N >> M;
  ll ans = COM((N) * 2, N) * 2 * N % mod;
  for (ll i = 0; i < M; ++i) {
    ll t, x, y;
    cin >> t >> x >> y;
    ll lx = N - x, ly = N - y;
    if (t == 1) {
      lx -= 1;
    }
    else {
      ly -= 1;
    }
    ans -= COM(x + y, x) * COM(lx + ly, lx) % mod;
    ans %= mod;
    if (ans < 0) {
      ans += mod;
    }
  }
  cout << ans << "\n";
}
0