結果
問題 | No.2388 At Least K-Characters |
ユーザー |
![]() |
提出日時 | 2023-07-22 10:07:37 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 299 ms / 4,000 ms |
コード長 | 3,039 bytes |
コンパイル時間 | 1,729 ms |
コンパイル使用メモリ | 178,040 KB |
実行使用メモリ | 124,896 KB |
最終ジャッジ日時 | 2024-07-05 04:15:49 |
合計ジャッジ時間 | 9,057 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#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 modmint 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];}}vector<vector<mint>> memo(30,vector<mint> (30));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++) {memo[count][j]+=dp[i+1][j]*down;memo[count+1][j]+=dp[i+1][j]*up;}if(!note[k]) {note[k]=true;count++;}if(count>=K&&i<N-1) {ans+=1;}}for(ll i=1;i<=26;i++) {for(ll j=0;j<=26;j++) {for(ll h=0;h<=j;h++) {if(i+h<K||i<j-h) continue;ans+=memo[i][j]*nCk(26-i,h)*nCk(i,j-h);}}}cout<<ans<<endl;}