結果

問題 No.2388 At Least K-Characters
ユーザー tabrtabr
提出日時 2023-07-21 22:55:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,996 bytes
コンパイル時間 2,291 ms
コンパイル使用メモリ 210,816 KB
実行使用メモリ 391,556 KB
最終ジャッジ日時 2023-09-18 12:57:26
合計ジャッジ時間 8,414 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,756 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 4 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 33 ms
4,940 KB
testcase_14 AC 27 ms
4,592 KB
testcase_15 AC 26 ms
4,728 KB
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

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 dp(27, vector<vector<int>>(27));
    dp[0][0] = vector<int>(m + 1);
    dp[0][0][0] = 1;
    for (int i = 0; i < 27; i++) {
        for (int j = i; i + j < 27; j++) {
            if (i + j > 26 || i + j == 0) {
                continue;
            }
            dp[i][j] = vector<int>(m + 1);
            for (int l = 1; l < m + 1; l++) {
                dp[i][j][l] = (int) (mint(dp[i][j][l - 1]) * (i + j)).value;
                if (i) {
                    dp[i][j][l] += dp[min(i - 1, j)][max(i - 1, j)][l - 1];
                    dp[i][j][l] %= (int) mod;
                }
                if (j) {
                    dp[i][j][l] += dp[min(i, j - 1)][max(i, j - 1)][l - 1];
                    dp[i][j][l] %= (int) mod;
                }
            }
        }
    }
    for (int i = 0; i < 27; i++) {
        for (int j = i; i + j < 27; j++) {
            if (i + j > 26) {
                continue;
            }
            for (int l = 1; l < m + 1; l++) {
                dp[i][j][l] += dp[i][j][l - 1];
                dp[i][j][l] %= (int) mod;
            }
        }
    }
    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[min(l, r)][max(l, r)][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