結果

問題 No.3589 Make Ends Meet (Hard)
コンテスト
ユーザー marc2825
提出日時 2026-05-29 16:56:01
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 222 ms / 2,000 ms
コード長 4,191 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,458 ms
コンパイル使用メモリ 227,248 KB
実行使用メモリ 9,088 KB
最終ジャッジ日時 2026-07-10 20:55:16
合計ジャッジ時間 4,602 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

static const int MOD = 998244353;

ll modpow(ll a, ll e) {
    ll r = 1;
    while (e > 0) {
        if (e & 1) r = r * a % MOD;
        a = a * a % MOD;
        e >>= 1;
    }
    return r;
}

struct Comb {
    vector<int> fact, ifact;

    void init(int n) {
        fact.assign(n + 1, 1);
        ifact.assign(n + 1, 1);
        for (int i = 1; i <= n; i++) fact[i] = (ll)fact[i - 1] * i % MOD;
        ifact[n] = (int)modpow(fact[n], MOD - 2);
        for (int i = n; i >= 1; i--) ifact[i - 1] = (ll)ifact[i] * i % MOD;
    }

    int C(int n, int r) const {
        if (r < 0 || r > n) return 0;
        return (ll)fact[n] * ifact[r] % MOD * ifact[n - r] % MOD;
    }
};

vector<int> mul_qs_minus_1_full(const vector<int> &p, int s, int E) {
    vector<int> res(E + 1, 0);

    // p(q) * q^s
    if (s <= E) {
        for (int i = 0; i + s <= E; i++) {
            res[i + s] = p[i];
        }
    }

    // - p(q)
    for (int i = 0; i <= E; i++) {
        res[i] -= p[i];
        if (res[i] < 0) res[i] += MOD;
    }

    return res;
}

void add_shift_scaled_full(vector<int> &dst, const vector<int> &src, int shift, int scale, int E) {
    if (scale == 0 || shift > E) return;

    for (int i = 0; i + shift <= E; i++) {
        if (src[i] == 0) continue;
        dst[i + shift] = (dst[i + shift] + (ll)src[i] * scale) % MOD;
    }
}

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

    int N, M, K;
    cin >> N >> M >> K;

    int E = N * (N - 1) / 2;
    int R = E - M;
    int S = N - 2;

    Comb comb;
    comb.init(E);

    // dp[r][s] is a polynomial in q of fixed length E+1.
    // active[r][s] indicates whether the state has appeared.
    vector<vector<vector<int>>> dp(S + 1, vector<vector<int>>(S + 2, vector<int>(E + 1, 0)));
    vector<vector<char>> active(S + 1, vector<char>(S + 2, 0));

    dp[S][1][0] = 1;
    active[S][1] = 1;

    // Build layers 1,2,...,K-1 without reaching vertex N.
    for (int step = 0; step < K - 1; step++) {
        vector<vector<vector<int>>> ndp(S + 1, vector<vector<int>>(S + 2, vector<int>(E + 1, 0)));
        vector<vector<char>> nactive(S + 1, vector<char>(S + 2, 0));

        for (int r = 0; r <= S; r++) {
            for (int s = 0; s <= S + 1; s++) {
                if (!active[r][s]) continue;

                vector<int> cur = dp[r][s]; // cur * (q^s - 1)^t

                for (int t = 0; t <= r; t++) {
                    int shift = t * (t - 1) / 2;
                    int ways = comb.C(r, t);

                    add_shift_scaled_full(ndp[r - t][t], cur, shift, ways, E);
                    nactive[r - t][t] = 1;

                    if (t != r) {
                        cur = mul_qs_minus_1_full(cur, s, E);
                    }
                }
            }
        }

        dp.swap(ndp);
        active.swap(nactive);
    }

    // H(q): generating polynomial for dist(1,N)=K in q=1+x.
    vector<int> H(E + 1, 0);

    // N must have at least one edge to the current frontier: (q^s - 1).
    // All edges among the remaining r vertices, between them and N,
    // and between them and the current frontier are free.
    for (int r = 0; r <= S; r++) {
        for (int s = 0; s <= S + 1; s++) {
            if (!active[r][s]) continue;

            const vector<int> &p = dp[r][s];
            int base = s * r + r + r * (r - 1) / 2;

            for (int i = 0; i <= E; i++) {
                int val = p[i];
                if (val == 0) continue;

                if (i + base + s <= E) {
                    H[i + base + s] += val;
                    if (H[i + base + s] >= MOD) H[i + base + s] -= MOD;
                }

                if (i + base <= E) {
                    H[i + base] -= val;
                    if (H[i + base] < 0) H[i + base] += MOD;
                }
            }
        }
    }

    // We need [x^R] H(1+x). If H(q)=sum_c h_c q^c, then
    // [x^R] H(1+x) = sum_c h_c * C(c,R).
    ll ans = 0;
    for (int c = R; c <= E; c++) {
        if (H[c] == 0) continue;
        ans += (ll)H[c] * comb.C(c, R) % MOD;
        ans %= MOD;
    }

    cout << ans << '\n';
    return 0;
}
0