結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー bokusunnybokusunny
提出日時 2022-01-20 12:02:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 1,563 bytes
コンパイル時間 2,119 ms
コンパイル使用メモリ 206,876 KB
実行使用メモリ 17,860 KB
最終ジャッジ日時 2023-08-15 08:52:19
合計ジャッジ時間 4,539 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
15,192 KB
testcase_01 AC 20 ms
15,304 KB
testcase_02 AC 70 ms
17,688 KB
testcase_03 AC 70 ms
17,612 KB
testcase_04 AC 70 ms
17,692 KB
testcase_05 AC 70 ms
17,568 KB
testcase_06 AC 71 ms
17,620 KB
testcase_07 AC 69 ms
17,564 KB
testcase_08 AC 68 ms
17,564 KB
testcase_09 AC 70 ms
17,620 KB
testcase_10 AC 70 ms
17,688 KB
testcase_11 AC 64 ms
17,580 KB
testcase_12 AC 64 ms
17,860 KB
testcase_13 AC 63 ms
17,616 KB
testcase_14 AC 20 ms
15,192 KB
testcase_15 AC 20 ms
15,256 KB
testcase_16 AC 20 ms
15,288 KB
testcase_17 AC 21 ms
15,244 KB
testcase_18 AC 22 ms
15,252 KB
testcase_19 AC 21 ms
15,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define bokusunny ios::sync_with_stdio(false), cin.tie(nullptr);

#include <atcoder/modint>
using namespace atcoder;

using mint = modint1000000007;
const int MOD = 1e9 + 7;

template <class T>
struct ModComb {
 private:
  vector<T> Fac, Finv, Inv;
  int MAX_N;

 public:
  ModComb(int max_n = 1 << 20) {
    MAX_N = max_n;
    Fac.resize(MAX_N + 1), Finv.resize(MAX_N + 1), Inv.resize(MAX_N + 1);
    Fac[0] = Fac[1] = 1;
    Finv[0] = Finv[1] = 1;
    Inv[1] = 1;

    for (int i = 2; i <= MAX_N; i++) {
      Fac[i] = Fac[i - 1] * i;
      Inv[i] = (mint)0 + MOD - Inv[MOD % i] * (MOD / i);
      Finv[i] = Finv[i - 1] * Inv[i];
    }
  }

  T nCk(int n, int k) {
    assert(n <= MAX_N);
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;

    return Fac[n] * Finv[k] * Finv[n - k];
  }

  T nHr(int n, int r) { return nCk(n + r - 1, r); }

  T nPr(int n, int r) {
    assert(n <= MAX_N);
    assert(0 <= r && r <= n);
    return Fac[n] / Fac[n - r];
  }
};

void solve() {
  int N, M;
  cin >> N >> M;
  vector<int> T(M), X(M), Y(M);
  for (int i = 0; i < M; i++) cin >> T[i] >> X[i] >> Y[i];

  ModComb<mint> Comb;

  mint ans = (mint)1 * 2 * N * Comb.nCk(2 * N, N);
  for (int i = 0; i < M; i++) {
    if (T[i] == 1) {
      ans -= Comb.nCk(X[i] + Y[i], X[i]) * Comb.nCk(2 * N - X[i] - Y[i] - 1, N - Y[i]);
    } else {
      ans -= Comb.nCk(X[i] + Y[i], X[i]) * Comb.nCk(2 * N - X[i] - Y[i] - 1, N - X[i]);
    }
  }

  cout << ans.val() << endl;
}

int main() {
  bokusunny;
  solve();

  return 0;
}
0