結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー trineutrontrineutron
提出日時 2021-07-09 21:57:18
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 267 ms / 2,000 ms
コード長 941 bytes
コンパイル時間 2,292 ms
コンパイル使用メモリ 199,192 KB
最終ジャッジ日時 2025-01-22 21:38:55
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/modint>

using namespace std;
using namespace atcoder;

using mint = modint1000000007;

vector<mint> fact, inv;

void init(int n)
{
    fact.push_back(1);
    inv.push_back(1);
    for (int i = 0; i < n; i++)
    {
        fact.push_back(fact.at(i) * (i + 1));
        inv.push_back(fact.at(i + 1).inv());
    }
}

mint ncr(int n, int r)
{
    if (r < 0 or n < r)
    {
        return 0;
    }
    return fact.at(n) * inv.at(r) * inv.at(n - r);
}

int main()
{
    int n, m;
    cin >> n >> m;
    init(2 * n);
    mint ans = 2 * n * ncr(2 * n, n);
    for (int i = 0; i < m; i++)
    {
        int t, x, y;
        cin >> t >> x >> y;
        if (t == 1)
        {
            ans -= ncr(x + y, x) * ncr(2 * n - x - y - 1, n - x - 1);
        }
        else
        {
            ans -= ncr(x + y, x) * ncr(2 * n - x - y - 1, n - x);
        }
    }
    cout << ans.val() << endl;
    return 0;
}
0