結果

問題 No.1691 Badugi
ユーザー KudeKude
提出日時 2021-09-24 23:34:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 4,499 bytes
コンパイル時間 4,661 ms
コンパイル使用メモリ 262,412 KB
実行使用メモリ 11,500 KB
最終ジャッジ日時 2023-09-18 22:25:59
合計ジャッジ時間 5,329 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
11,324 KB
testcase_01 AC 15 ms
11,500 KB
testcase_02 AC 15 ms
11,224 KB
testcase_03 AC 15 ms
11,496 KB
testcase_04 AC 15 ms
11,440 KB
testcase_05 AC 15 ms
11,280 KB
testcase_06 AC 15 ms
11,384 KB
testcase_07 AC 15 ms
11,236 KB
testcase_08 AC 15 ms
11,440 KB
testcase_09 AC 14 ms
11,240 KB
testcase_10 AC 14 ms
11,324 KB
testcase_11 AC 15 ms
11,292 KB
testcase_12 AC 15 ms
11,272 KB
testcase_13 AC 14 ms
11,300 KB
testcase_14 AC 15 ms
11,376 KB
testcase_15 AC 15 ms
11,252 KB
testcase_16 AC 15 ms
11,280 KB
testcase_17 AC 15 ms
11,444 KB
testcase_18 AC 14 ms
11,304 KB
testcase_19 AC 15 ms
11,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) {a = max(a, b);}
template<class T> void chmin(T& a, const T& b) {a = min(a, b);}
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint998244353;

constexpr int FACT_SIZE = 1000000;
mint Fact[FACT_SIZE + 1];
mint iFact[FACT_SIZE + 1];
const auto fact_init = [] {
    Fact[0] = mint::raw(1);
    for(int i = 1; i <= FACT_SIZE; ++i) {
        Fact[i] = Fact[i-1] * i;
    }
    iFact[FACT_SIZE] = Fact[FACT_SIZE].inv();
    for(int i = FACT_SIZE; i; --i) {
        iFact[i-1] = iFact[i] * i;
    }
    return false;
}();

mint comb(int n, int k) {
    if (k == 0) return mint::raw(1);
    assert(n >= 0 && k >= 0);
    if (k > n) return mint::raw(0);
    return Fact[n] * iFact[n - k] * iFact[k];
}

mint icomb(int n, int k) {
    return iFact[n] * Fact[n - k] * Fact[k];
}

mint fact(int n) {return Fact[n];}
mint perm(int n, int k) {
    if (n < k) return 0;
    assert(0 <= n);
    return Fact[n] * iFact[n - k];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, k;
    cin >> n >> m >> k;
    mint ans;
    // mint pre;
    if (n >= 3 && m >= 3 && k >= 5) {
        ans += comb(n, k - 2) * perm(m, k - 2) * perm(k - 2, 3);
        // cerr << (ans - pre).val() << endl;
        // pre = ans;
    }
    rep(_, 2) {
        if (n >= 3 && k >= 4) {
            ans += perm(n, 3) * perm(m, 2) / 2 * comb(n - 3, k - 4) * perm(m - 2, k - 4);
            // cerr << (ans - pre).val() << endl;
            // pre = ans;
        }
        swap(n, m);
    }
    if (k >= 4) {
        // ans += comb(n, 2)* comb(m, 2) * comb(n - 2, k - 4) * perm(m - 2, k - 4);
        ans += comb(n, k - 2) * perm(m, k - 2) * comb(k - 2, 2) / 2;
        // cerr << (ans - pre).val() << endl;
        // pre = ans;
    }
    if (k >= 6 && n >= 4 && m >= 4) {
        // ans += comb(n, k) * perm(m, k) * comb(k, 2) * comb(k - 2, 2) / 2;
        ans += comb(n, k - 2) * perm(m, k - 2) * perm(k - 2, 2) * perm(k - 4, 2) / 2;
        // cerr << (ans - pre).val() << endl;
        // pre = ans;
    }
    rep(_, 2) {
        if (k >= 5 && n >= 4 && m >= 3) {
            ans += perm(n, 2) * perm(m, 2) * comb(n - 2, 2) * (m - 2) * comb(n - 4, k - 5) * perm(m - 3, k - 5);
            // cerr << (ans - pre).val() << endl;
            // pre = ans;
        }
        if (k >= 4 && n >= 4) {
            ans += comb(n, 2) * m * comb(n - 2, 2) * (m - 1) / 2 * comb(n - 4, k - 4) * perm(m - 2, k - 4);
            // cerr << (ans - pre).val() << endl;
            // pre = ans;
        }
        swap(n, m);
    }
    if (k >= 4 && n >= 3 && m >= 3) {
        ans += comb(n, 2) * (n - 2) * comb(m, 2) * (m - 2) * comb(n - 3, k - 4) * perm(m - 3, k - 4);
        // cerr << (ans - pre).val() << endl;
        // pre = ans;
    }
    rep(_, 2) {
        if (n >= 3) {
            ans += comb(n, 3) * m * comb(n - 3, k - 3) * perm(m - 1, k - 3);
        }
        if (n >= 3 && k >= 4) {
            ans += comb(n, 3) * perm(m, 2) * 3 * comb(n - 3, k - 4) * perm(m - 2, k - 4);
        }
        if (n >= 3 && m >= 3 && k >= 5) {
            // ans += comb(n, 3) * m * comb(m - 1, 2) * 3 * 2 * comb(n - 3, k - 5) * perm(m - 3, k - 5);
            ans += perm(n, 3) * perm(m, 2) / 2 * (m - 2) * comb(n - 3, k - 5) * perm(m - 3, k - 5);
        }
        swap(n, m);
    }
    // {
    //     int cnt = 0;
    //     rep(s, 1 << n * m) {
    //         if (__builtin_popcount(s) != k) continue;
    //         int from = n * m, to = from + 1;
    //         mf_graph<int> g(n * m + 2);
    //         rep(i, n) rep(j, m) if (s >> (i * m + j) & 1) g.add_edge(i,n+j,1);
    //         rep(i, n) g.add_edge(from, i, 1);
    //         rep(j, m) g.add_edge(n + j, to, 1);
    //         int f = g.flow(from, to);
    //         if (f == k - 2) {
    //             cout << s << endl;
    //             rep(i, n) rep(j, m) if (s >> (i * m + j) & 1) {
    //                 cout << i << ' ' << j << endl;
    //             }
    //             cout << endl;
    //             cnt++;
    //         }
    //     }
    //     cout << cnt << endl;
    // }
    cout << ans.val() << endl;
}
0