結果

問題 No.2388 At Least K-Characters
ユーザー tabrtabr
提出日時 2023-07-21 22:46:00
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 4,590 bytes
コンパイル時間 1,858 ms
コンパイル使用メモリ 204,636 KB
最終ジャッジ日時 2025-02-15 17:17:06
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 13 MLE * 1 -- * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

template <long long mod>
struct modular {
    long long value;
    modular(long long x = 0) {
        value = x % mod;
        if (value < 0) value += mod;
    }
    modular& operator+=(const modular& other) {
        if ((value += other.value) >= mod) value -= mod;
        return *this;
    }
    modular& operator-=(const modular& other) {
        if ((value -= other.value) < 0) value += mod;
        return *this;
    }
    modular& operator*=(const modular& other) {
        value = value * other.value % mod;
        return *this;
    }
    modular& operator/=(const modular& other) {
        long long a = 0, b = 1, c = other.value, m = mod;
        while (c != 0) {
            long long t = m / c;
            m -= t * c;
            swap(c, m);
            a -= t * b;
            swap(a, b);
        }
        a %= mod;
        if (a < 0) a += mod;
        value = value * a % mod;
        return *this;
    }
    friend modular operator+(const modular& lhs, const modular& rhs) { return modular(lhs) += rhs; }
    friend modular operator-(const modular& lhs, const modular& rhs) { return modular(lhs) -= rhs; }
    friend modular operator*(const modular& lhs, const modular& rhs) { return modular(lhs) *= rhs; }
    friend modular operator/(const modular& lhs, const modular& rhs) { return modular(lhs) /= rhs; }
    modular& operator++() { return *this += 1; }
    modular& operator--() { return *this -= 1; }
    modular operator++(int) {
        modular res(*this);
        *this += 1;
        return res;
    }
    modular operator--(int) {
        modular res(*this);
        *this -= 1;
        return res;
    }
    modular operator-() const { return modular(-value); }
    bool operator==(const modular& rhs) const { return value == rhs.value; }
    bool operator!=(const modular& rhs) const { return value != rhs.value; }
    bool operator<(const modular& rhs) const { return value < rhs.value; }
};
template <long long mod>
string to_string(const modular<mod>& x) {
    return to_string(x.value);
}
template <long long mod>
ostream& operator<<(ostream& stream, const modular<mod>& x) {
    return stream << x.value;
}
template <long long mod>
istream& operator>>(istream& stream, modular<mod>& x) {
    stream >> x.value;
    x.value %= mod;
    if (x.value < 0) x.value += mod;
    return stream;
}

constexpr long long mod = 998244353;
using mint = modular<mod>;

mint power(mint a, long long n) {
    mint res = 1;
    while (n > 0) {
        if (n & 1) {
            res *= a;
        }
        a *= a;
        n >>= 1;
    }
    return res;
}

vector<mint> fact(1, 1);
vector<mint> finv(1, 1);

mint C(int n, int k) {
    if (n < k || k < 0) {
        return mint(0);
    }
    while ((int) fact.size() < n + 1) {
        fact.emplace_back(fact.back() * (int) fact.size());
        finv.emplace_back(mint(1) / fact.back());
    }
    return fact[n] * finv[k] * finv[n - k];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m, w;
    cin >> n >> m >> w;
    string s;
    cin >> s;
    int t = 0;
    mint ans = 0;
    vector<vector<vector<mint>>> dp(27, vector<vector<mint>>(27, vector<mint>(m + 1)));
    dp[0][0][0] = 1;
    for (int i = 0; i < 27; i++) {
        for (int j = 0; j < 27; j++) {
            for (int l = 1; l < m + 1; l++) {
                dp[i][j][l] = dp[i][j][l - 1] * (i + j);
                if (i) {
                    dp[i][j][l] += dp[i - 1][j][l - 1];
                }
                if (j) {
                    dp[i][j][l] += dp[i][j - 1][l - 1];
                }
            }
        }
    }
    for (int i = 0; i < 27; i++) {
        for (int j = 0; j < 27; j++) {
            for (int l = 1; l < m + 1; l++) {
                dp[i][j][l] += dp[i][j][l - 1];
            }
        }
    }
    C(30, 0);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < s[i] - 'a'; j++) {
            int u = t | (1 << j);
            u = __builtin_popcount(u);
            for (int l = 0; l <= 26 - u; l++) {
                for (int r = 0; r <= u; r++) {
                    if (u + l < w) {
                        continue;
                    }
                    ans += dp[r][l][m - 1 - i] * C(u, r) * fact[r] * C(26 - u, l) * fact[l];
                }
            }
            // debug(j, ans);
        }
        t |= 1 << (s[i] - 'a');
        if (__builtin_popcount(t) >= w && i < n - 1) {
            ans++;
        }
    }
    cout << ans << '\n';
    return 0;
}
0