結果

問題 No.1596 Distance Sum in 2D Plane
コンテスト
ユーザー toppo57
提出日時 2026-10-29 16:43:37
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc16.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 39 ms / 2,000 ms
+ 676µs
コード長 2,186 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,004 ms
コンパイル使用メモリ 175,028 KB
実行使用メモリ 9,728 KB
最終ジャッジ日時 2026-10-29 16:44:03
合計ジャッジ時間 5,046 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;

const int dx[]={0, 1, 0}, dy[]={0, 0, 1};

const int mod = 1e9+7;
const int MAX = 400010;
class mint {
    long long x;
public:
    mint(long long x=0) : x((x%mod+mod)%mod) {}
    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(long long t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    
    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;
    }

    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
};

vector<mint> fact(MAX), finv(MAX);

void init_nCr() {
    fact[0] = 1;
    for (int i = 1; i < MAX; i++) {
        fact[i] = fact[i - 1] * i;
    }
    
    finv[MAX - 1] = fact[MAX - 1].inv();
    
    for (int i = MAX - 1; i > 0; i--) {
        finv[i - 1] = finv[i] * i;
    }
    return;
}

mint combi(int n, int k) {
    if (n < k || n < 0 || k < 0) return 0;
    return fact[n] * finv[k] * finv[n - k];
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    init_nCr();

    int N, M; cin>>N>>M;
    mint ans = combi(2*N, N)*2*N;

    for (int i=0;i<M;i++){
        int t, x, y; cin>>t>>x>>y;
        int nx=x+dx[t], ny=y+dy[t];
        ans -= combi(x+y, x)*combi(2*N-nx-ny, N-nx);
    }

    cout << ans << "\n";
    return 0;
}
0