結果

問題 No.2802 Pill Bug in Grid Maze
ユーザー NachiaNachia
提出日時 2024-07-12 00:00:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 2,384 bytes
コンパイル時間 1,295 ms
コンパイル使用メモリ 101,904 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2024-07-12 00:00:08
合計ジャッジ時間 3,583 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 6 ms
5,376 KB
testcase_08 AC 78 ms
6,400 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 7 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 5 ms
5,376 KB
testcase_20 AC 27 ms
5,376 KB
testcase_21 AC 53 ms
5,376 KB
testcase_22 AC 28 ms
5,376 KB
testcase_23 AC 66 ms
5,376 KB
testcase_24 AC 33 ms
5,376 KB
testcase_25 AC 14 ms
5,376 KB
testcase_26 AC 13 ms
5,376 KB
testcase_27 AC 8 ms
5,376 KB
testcase_28 AC 27 ms
5,376 KB
testcase_29 AC 46 ms
5,376 KB
testcase_30 AC 10 ms
5,376 KB
testcase_31 AC 76 ms
6,016 KB
testcase_32 AC 67 ms
5,376 KB
testcase_33 AC 22 ms
5,376 KB
testcase_34 AC 35 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <array>
#include <cmath>
#include <atcoder/modint>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<int(n); i++)
#define repr(i,n) for(int i=int(n)-1; i>=0; i--)
using Modint = atcoder::static_modint<998244353>;

namespace nachia{

template<class Modint>
class Comb{
private:
    std::vector<Modint> F;
    std::vector<Modint> iF;
public:
    void extend(int newN){
        int prevN = (int)F.size() - 1;
        if(prevN >= newN) return;
        F.resize(newN+1);
        iF.resize(newN+1);
        for(int i=prevN+1; i<=newN; i++) F[i] = F[i-1] * Modint::raw(i);
        iF[newN] = F[newN].inv();
        for(int i=newN; i>prevN; i--) iF[i-1] = iF[i] * Modint::raw(i);
    }
    Comb(int n = 1){
        F.assign(2, Modint(1));
        iF.assign(2, Modint(1));
        extend(n);
    }
    Modint factorial(int n) const { return F[n]; }
    Modint invFactorial(int n) const { return iF[n]; }
    Modint invOf(int n) const { return iF[n] * F[n-1]; }
    Modint comb(int n, int r) const {
        if(n < 0 || n < r || r < 0) return Modint(0);
        return F[n] * iF[r] * iF[n-r];
    }
    Modint invComb(int n, int r) const {
        if(n < 0 || n < r || r < 0) return Modint(0);
        return iF[n] * F[r] * F[n-r];
    }
    Modint perm(int n, int r) const {
        if(n < 0 || n < r || r < 0) return Modint(0);
        return F[n] * iF[n-r];
    }
    Modint invPerm(int n, int r) const {
        if(n < 0 || n < r || r < 0) return Modint(0);
        return iF[n] * F[n-r];
    }
    Modint operator()(int n, int r) const { return comb(n,r); }
};

} // namespace nachia
using namespace std;

void testcase(){
    int H, W; cin >> H >> W;
    auto comb = nachia::Comb<Modint>(H+W);
    if(H == 1 || W == 1){ cout << "1\n"; return; }
    Modint ans = 0;
    auto f = [&](i64 k) -> Modint {
        if(k > i64(H) * W) return 0;
        return Modint(2).pow(i64(H)*W - k);
    };
    for(int K=0; K<=W-1; K++){
        auto x = comb(W-1, K);
        auto a = comb(H-2, K);
        auto b = comb(H-2, K-1);
        ans += x * a * f(H+W-1+K*2-0);
        ans += x * b * f(H+W-1+K*2-1);
    }
    cout << ans.val() << endl;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    testcase();
    return 0;
}
0