結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー tarattata1tarattata1
提出日時 2021-07-09 22:03:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 127 ms / 2,000 ms
コード長 2,501 bytes
コンパイル時間 780 ms
コンパイル使用メモリ 92,684 KB
実行使用メモリ 9,756 KB
最終ジャッジ日時 2023-09-14 09:08:31
合計ジャッジ時間 4,172 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
9,652 KB
testcase_01 AC 61 ms
9,656 KB
testcase_02 AC 125 ms
9,092 KB
testcase_03 AC 127 ms
9,128 KB
testcase_04 AC 127 ms
9,088 KB
testcase_05 AC 125 ms
9,704 KB
testcase_06 AC 123 ms
9,096 KB
testcase_07 AC 127 ms
9,596 KB
testcase_08 AC 127 ms
9,096 KB
testcase_09 AC 127 ms
9,656 KB
testcase_10 AC 127 ms
9,476 KB
testcase_11 AC 112 ms
9,688 KB
testcase_12 AC 112 ms
9,756 KB
testcase_13 AC 112 ms
9,624 KB
testcase_14 AC 61 ms
9,556 KB
testcase_15 AC 62 ms
9,608 KB
testcase_16 AC 61 ms
9,132 KB
testcase_17 AC 62 ms
9,140 KB
testcase_18 AC 62 ms
9,088 KB
testcase_19 AC 61 ms
9,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <cassert>
#include <numeric>
#include <functional>
#include <ctime>
#include <bitset>
#pragma warning(disable:4996) 

//#define ATCODER
#ifdef ATCODER
#include <atcoder/all>
#endif

typedef long long ll;
typedef unsigned long long ull;
#define LINF  9223300000000000000
#define LINF2 1223300000000000000
#define LINF3 1000000000000
#define INF 2140000000
const long long MOD = 1000000007;
//const long long MOD = 998244353;

using namespace std;
#ifdef ATCODER
using namespace atcoder;
#endif


const int max_comb = 410000;
vector<ll> fac(max_comb + 1); //n! (mod M)
vector<ll> ifac(max_comb + 1); //k!^(-1) (mod M)

ll mpow(ll x, ll n) { //x^n(mod M)
    ll ans = 1;
    while (n != 0) {
        if (n & 1) ans = ans * x % MOD;
        x = x * x % MOD;
        n = n >> 1;
    }
    return ans;
}

ll minv(ll x) {
    return mpow(x, MOD - 2);
}

ll comb(int a, int b) {     // C(a,b) = a! * b!^(-1) * (a-b)^(-1)
    if (a == 0 && b == 0)return 1;
    if (a < b || a < 0)return 0;
    ll tmp = ifac[a - b] * ifac[b] % MOD;
    return tmp * fac[a] % MOD;
}

ll perm(int a, int b) {     // P(a,b) = a! * (a-b)!^(-1)
    if (b == 0)return 1;
    if (a < b || a < 0)return 0;
    ll tmp = ifac[a - b] % MOD;
    return tmp * fac[a] % MOD;
}

void pre_comb()
{
    fac[0] = 1;
    ifac[0] = 1;
    for (int i = 0; i < max_comb; i++) {
        fac[i + 1] = fac[i] * (i + 1) % MOD; // n!(mod M)
        ifac[i + 1] = ifac[i] * minv(i + 1) % MOD; // k!^(-1) (mod M)
    }
    return;
}


void solve()
{
    pre_comb();
    int n, m;
    scanf("%d%d", &n, &m);
    ll ans0 = 0;
    for (int i = 0; i < m; i++) {
        int t, x, y;
        scanf("%d%d%d", &t, &x, &y);
        if (t == 1) {
            ll tmp = comb(x + y, x)*comb(n - x - 1 + n - y, n - x - 1)%MOD;
            ans0 = (ans0 + tmp) % MOD;
        }
        else {
            ll tmp = comb(x + y, x)*comb(n - x - 1 + n - y, n - x) % MOD;
            ans0 = (ans0 + tmp) % MOD;
        }
    }
    ll ans = comb(n * 2, n)*n * 2 % MOD;
    ans = (ans - ans0 + MOD) % MOD;
    printf("%lld\n", ans);

    return;
}

int main()
{
#if 1
    solve();
#else
    int T, t;
    scanf("%d", &T);
    for (t = 0; t < T; t++) {
        //printf("Case #%d: ", t + 1);
        solve();
    }
#endif
    return 0;
}
0