結果
問題 |
No.2388 At Least K-Characters
|
ユーザー |
![]() |
提出日時 | 2023-07-22 09:41:33 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,020 bytes |
コンパイル時間 | 1,779 ms |
コンパイル使用メモリ | 176,840 KB |
実行使用メモリ | 131,956 KB |
最終ジャッジ日時 | 2024-07-05 04:15:39 |
合計ジャッジ時間 | 8,023 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 13 TLE * 1 -- * 20 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll =long long; #define all(v) v.begin(),v.end() #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) ll INF=2e18; const int mod = 998244353; class mint { long long x; public: mint(long long x=0) : x((x%mod+mod)%mod) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint& a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint& a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint& a) { (x *= a.x) %= mod; 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; } // for prime mod mint inv() const { return pow(mod-2); } mint& operator/=(const mint& a) { return (*this) *= a.inv(); } mint operator/(const mint& a) const { mint res(*this); return res/=a; } friend ostream& operator<<(ostream& os, const mint& m){ os << m.x; return os; } }; const int MOD = 998244353; vector<mint> fact, fact_inv, inv; void init_nCk(int SIZE) { fact.resize(SIZE + 5); fact_inv.resize(SIZE + 5); inv.resize(SIZE + 5); fact[0] = fact[1] = 1; fact_inv[0] = fact_inv[1] = 1; inv[1] = 1; for (int i = 2; i < SIZE + 5; i++) { fact[i] = fact[i - 1] * i ; inv[i] = - inv[MOD % i] * (MOD / i) +MOD; fact_inv[i] = fact_inv[i - 1] * inv[i] ; } } mint nCk(int n, int k) { if((n < k)) return 0; if ((n < 0 || k < 0)) return 0; return fact[n] * (fact_inv[k] * fact_inv[n - k] ); } int main() { ios::sync_with_stdio(false); cin.tie(0); ll N,M,K;cin>>N>>M>>K; string S;cin>>S; init_nCk(100); vector<vector<mint>> dp(M+10,vector<mint> (27,0LL)); dp[M][0]=1; for(ll i=M-1;i>=0;i--) { for(ll j=1;j<=26;j++) { dp[i][j]+=dp[i+1][j-1]*j+dp[i+1][j]*j; } } for(ll i=M-1;i>=0;i--) { for(ll j=0;j<=26;j++) { dp[i][j]+=dp[i+1][j]; } } mint ans=0; ll count=0; vector<bool> note(26); for(ll i=0;i<N;i++) { ll k=S[i]-'a'; ll up=0; ll down=0; for(ll j=0;j<k;j++) { if(!note[j]) up++; else down++; } for(ll j=0;j<=26;j++) { for(ll h=0;h<=j;h++) { if(j-h>count+1||count+1+h<K) {} else{ ll noko=26-count-1; ans+=dp[i+1][j]*nCk(noko,h)*nCk(count+1,j-h)*up; } if(j-h>count||count+h<K){} else { ll noko=26-count; ans+=dp[i+1][j]*nCk(noko,h)*nCk(count,j-h)*down; } } } if(!note[k]) { note[k]=true; count++; } if(count>=K&&i<N-1) { ans+=1; } } cout<<ans<<endl; }