結果

問題 No.2388 At Least K-Characters
ユーザー srjywrdnprkt
提出日時 2023-08-08 12:50:19
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 313 ms / 4,000 ms
コード長 2,817 bytes
コンパイル時間 2,063 ms
コンパイル使用メモリ 200,116 KB
最終ジャッジ日時 2025-02-16 00:05:52
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll modc = 998244353;
class mint {
    ll x;
public:
    mint(ll x=0) : x((x%modc+modc)%modc) {}
    mint operator-() const {
      return mint(-x);
    }
    mint& operator+=(const mint& a) {
        if ((x += a.x) >= modc) x -= modc;
        return *this;
    }
    mint& operator-=(const mint& a) {
        if ((x += modc-a.x) >= modc) x -= modc;
        return *this;
    }
    mint& operator*=(const  mint& a) {
        (x *= a.x) %= modc;
        return *this;
    }
    mint operator+(const mint& a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint& a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint& a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    mint inv() const {
        return pow(modc-2);
    }
    mint& operator/=(const mint& a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint& a) const {
        mint res(*this);
        return res/=a;
    }
    bool operator == (const mint& a) const{
        return x == a.x;
    }
    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
    friend istream& operator>>(istream& ip, mint &m) {
        ll t;
        ip >> t;
        m = mint(t);
        return ip;
    }
    ll val(){
        return x;
    }
};

int main(){

    ll N, M, K, n, c, d, used=0;
    string S;
    cin >> N >> M >> K >> S;
    mint ans=0, cnt;
    vector<vector<mint>> dp(M+1, vector<mint>(27));
    dp[0][0] = 1;

    for (int i=0; i<M; i++){
    	d = __builtin_popcount(used);
        for (int j=0; j<=26; j++){
            cnt = dp[i][j]; //Sの接頭辞より真に小さい文字列の数
            if (i<N && j==d){
                c = S[i]-'a';
                n = __builtin_popcount(used & ((1<<c)-1));
                //Sの接頭辞からの遷移
                dp[i+1][j] += n;
                if (j+1<=26) dp[i+1][j+1] += c-n;
                if (used & (1<<c)) dp[i+1][j] += 1;
                else dp[i+1][j+1] += 1;
                cnt -= 1;
            }
            else if (i==N && j==d){
                cnt -= 1; //S自身
            }
            //Sの接頭辞より真に小さい文字列からの遷移
            dp[i+1][j] += cnt * j;
            if (j+1<=26) dp[i+1][j+1] += cnt * (26-j);
        }
        if (i<N) used |= 1<<(S[i]-'a');
    }

    for (int i=1; i<=M; i++){
        for (int j=K; j<=26; j++) ans += dp[i][j];
    }

    if (__builtin_popcount(used) >= K) ans -= 1; //Sを取り除く
    cout << ans << endl;

    return 0;
}

0