結果

問題 No.1520 Zigzag Sum
ユーザー vkgainzvkgainz
提出日時 2021-06-05 08:34:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 1,078 bytes
コンパイル時間 1,423 ms
コンパイル使用メモリ 171,484 KB
実行使用メモリ 11,112 KB
最終ジャッジ日時 2024-05-01 03:06:41
合計ジャッジ時間 3,537 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
11,064 KB
testcase_01 AC 19 ms
10,996 KB
testcase_02 AC 179 ms
11,112 KB
testcase_03 AC 217 ms
11,016 KB
testcase_04 AC 202 ms
11,028 KB
testcase_05 AC 205 ms
11,040 KB
testcase_06 AC 207 ms
10,968 KB
testcase_07 AC 204 ms
11,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<long long> fact, invfact;
long long MOD = 1e9 + 7;

long long inv(long long b, int p = MOD - 2) {
    if(p == 0) return 1LL;
    long long x = inv(b, p / 2);
    x = x * x % MOD;
    if(p % 2) x = x * b % MOD;
    return x;
}
int main() {
    int T; cin >> T;
    fact.resize(500001), invfact.resize(500001);
    fact[0] = 1LL;
    for(int i = 1; i <= 500000; i++) {
        fact[i] = fact[i - 1] * 1LL * i % MOD;
    }
    invfact.back() = inv(fact.back());
    for(int i = 499999; i >= 0; i--) {
        invfact[i] = invfact[i + 1] * 1LL * (i + 1) % MOD;
    }
    for(int tc = 1; tc <= T; tc++) {
        int H, W; cin >> H >> W;
        if(H == 1 || W == 1) {
            cout << 0 << "\n";
            continue;
        }
        long long ans = (H + W - 3);
        ans %= MOD;
        ans *= fact[H + W - 4];
        ans %= MOD;
        ans *= invfact[H - 2];
        ans %= MOD;
        ans *= invfact[W - 2];
        ans *= 2;
        ans %= MOD;
        if(ans < 0) ans += MOD;
        cout << ans << "\n";
    }
}
0