結果

問題 No.1520 Zigzag Sum
コンテスト
ユーザー toppo57
提出日時 2026-10-29 17:58:16
言語 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  
実行時間 30 ms / 2,000 ms
+ 941µs
コード長 2,123 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,283 ms
コンパイル使用メモリ 177,188 KB
実行使用メモリ 9,908 KB
最終ジャッジ日時 2026-10-29 17:58:24
合計ジャッジ時間 4,521 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 7
権限があれば一括ダウンロードができます

ソースコード

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 T; cin>>T;
    for (int scenario=0;scenario<T;scenario++){
        int H, W; cin>>H>>W;
        mint ans = combi(H+W-4, W-2)*(H+W-3)*2;
        cout << ans << "\n";
    }

    return 0;
}
0