結果

問題 No.1520 Zigzag Sum
ユーザー MisterMister
提出日時 2021-05-28 21:47:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 1,169 bytes
コンパイル時間 898 ms
コンパイル使用メモリ 79,488 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2024-04-24 23:58:19
合計ジャッジ時間 1,698 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
6,528 KB
testcase_01 AC 6 ms
6,528 KB
testcase_02 AC 27 ms
6,528 KB
testcase_03 AC 35 ms
6,528 KB
testcase_04 AC 34 ms
6,528 KB
testcase_05 AC 35 ms
6,400 KB
testcase_06 AC 31 ms
6,528 KB
testcase_07 AC 32 ms
6,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/modint>
#include <iostream>
#include <vector>

template <class T>
struct Combination {
    int max_n;
    std::vector<T> f, invf;

    explicit Combination(int n)
        : max_n(n), f(n + 1), invf(n + 1) {
        f[0] = 1;
        for (int i = 1; i <= n; ++i) {
            f[i] = f[i - 1] * i;
        }

        invf[max_n] = f[max_n].inv();
        for (int i = max_n - 1; i >= 0; --i) {
            invf[i] = invf[i + 1] * (i + 1);
        }
    }

    T fact(int n) const { return n < 0 ? T(0) : f[n]; }
    T invfact(int n) const { return n < 0 ? T(0) : invf[n]; }
    T perm(int a, int b) const {
        return a < b || b < 0 ? T(0) : f[a] * invf[a - b];
    }
    T binom(int a, int b) const {
        return a < b || b < 0 ? T(0) : f[a] * invf[a - b] * invf[b];
    }
};

using namespace std;
using mint = atcoder::modint1000000007;

const Combination<mint> C(400000);

void solve() {
    int h, w;
    cin >> h >> w;
    cout << (C.binom(h + w - 4, h - 2) * (h + w - 3) * 2).val()
         << "\n";
}

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

    int q;
    cin >> q;
    while (q--) solve();

    return 0;
}
0