結果

問題 No.1740 Alone 'a'
ユーザー nawawannawawan
提出日時 2021-11-12 22:14:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 3,392 bytes
コンパイル時間 1,941 ms
コンパイル使用メモリ 210,792 KB
実行使用メモリ 24,228 KB
最終ジャッジ日時 2024-05-04 09:10:38
合計ジャッジ時間 3,330 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
13,072 KB
testcase_01 AC 4 ms
13,116 KB
testcase_02 AC 5 ms
13,112 KB
testcase_03 AC 18 ms
24,180 KB
testcase_04 AC 18 ms
24,228 KB
testcase_05 AC 5 ms
13,388 KB
testcase_06 AC 5 ms
13,408 KB
testcase_07 AC 4 ms
13,256 KB
testcase_08 AC 5 ms
13,260 KB
testcase_09 AC 5 ms
13,140 KB
testcase_10 AC 4 ms
13,096 KB
testcase_11 AC 7 ms
15,052 KB
testcase_12 AC 15 ms
22,092 KB
testcase_13 AC 15 ms
21,568 KB
testcase_14 AC 17 ms
23,504 KB
testcase_15 AC 5 ms
13,128 KB
testcase_16 AC 14 ms
21,064 KB
testcase_17 AC 5 ms
13,132 KB
testcase_18 AC 7 ms
14,368 KB
testcase_19 AC 14 ms
22,036 KB
testcase_20 AC 11 ms
17,624 KB
testcase_21 AC 13 ms
19,320 KB
testcase_22 AC 14 ms
20,556 KB
testcase_23 AC 14 ms
20,548 KB
testcase_24 AC 10 ms
17,740 KB
testcase_25 AC 10 ms
17,864 KB
testcase_26 AC 12 ms
19,288 KB
testcase_27 AC 13 ms
19,788 KB
testcase_28 AC 11 ms
18,652 KB
testcase_29 AC 17 ms
23,632 KB
testcase_30 AC 13 ms
19,548 KB
testcase_31 AC 14 ms
20,936 KB
testcase_32 AC 11 ms
18,764 KB
testcase_33 AC 14 ms
20,940 KB
testcase_34 AC 7 ms
14,416 KB
testcase_35 AC 10 ms
18,312 KB
testcase_36 AC 10 ms
17,580 KB
testcase_37 AC 6 ms
14,664 KB
testcase_38 AC 10 ms
16,800 KB
testcase_39 AC 6 ms
13,512 KB
testcase_40 AC 9 ms
15,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<const int MOD> struct modint{
    long long val;
    modint(): val(0) {}
    modint(long long x){
        if(x < 0) val = x % MOD + MOD;
        else val = x % MOD;
    }
    modint(const modint &t){
        val = t.val;
    }
    modint& operator =(const modint m){
        val = m.val;
        return *this;
    }
    modint operator -(){
        return modint(-val);
    }
    modint& operator-=(const modint &m){
        val -= m.val;
        if(val < 0) val += MOD;
        return *this;
    }
    modint& operator+=(const modint &m){
        val += m.val;
        if(val >= MOD) val -= MOD;
        return *this;
    }
    modint& operator*=(const modint &m){
        val *= m.val;
        val %= MOD;
        return *this;
    }
    modint& operator/=(modint m){
        *this *= m.inv();
        return *this;
    }
    modint inv(){
        long long x = 1, y = 0;
        long long a = val, b = MOD;
        while(b != 0){
            long long t = a / b;
            a -= t * b;
            x -= t * y;
            swap(a, b);
            swap(x, y);
        }
        x %= MOD;
        if(x < 0) x += MOD;
        return modint(x);
    }
    modint pow(long long k){
        long long res = 1;
        long long v = val;
        while(k > 0){
            if(k & 1) res = res * v % MOD;
            v = v * v % MOD;
            k >>= 1;
    }
        return modint(res);
    }
    bool operator==(const modint &m){
        return val == m.val;
    }
    modint operator+(const modint &m){
        return modint(*this) += m;
    }
    modint operator-(const modint &m){
        return modint(*this) -= m;
    }
    modint operator*(const modint &m){
        return modint(*this) *= m;
    }
    modint operator/(const modint &m){
        return modint(*this) /= m;
    }
    bool operator!=(const modint &m){
        return modint(*this).val != m.val;
    }
    bool operator!=(const int &m){
        return modint(*this).val != m;
    }
};
const int MOD = 998244353;
using mint = modint<MOD>;
const int MAX = 410000;
mint fac[MAX], finv[MAX], inv[MAX];
void COMinit(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i;
        inv[i] = mint(MOD) - inv[MOD % i] * (MOD / i);
        finv[i] = finv[i - 1] * inv[i];
    }
}
mint COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * finv[k] * finv[n - k];
}
int main(){
    int N;
    cin >> N;
    string S;
    cin >> S;
    vector<vector<vector<mint>>> dp(2, vector<vector<mint>>(2, vector<mint>(N)));
    if(S[0] == 'a'){
        dp[0][1][0] = 1;
    }
    else{
        dp[0][0][0] = 1;
        dp[1][0][0] = S[0] - 'a' - 1;
        dp[1][1][0] = 1;
    }
    for(int i = 1; i < N; i++){
        if(S[i] == 'a'){
            dp[0][1][i] = dp[0][0][i - 1];
            dp[1][0][i] = dp[1][0][i - 1] * 25;
            dp[1][1][i] = dp[1][0][i - 1] + dp[1][1][i - 1] * 25;
        }
        else{
            dp[0][0][i] = dp[0][0][i - 1];
            dp[0][1][i] = dp[0][1][i - 1];
            dp[1][0][i] = dp[1][0][i - 1] * 25 + dp[0][0][i - 1] * (S[i] - 'a' - 1);
            dp[1][1][i] = dp[1][0][i - 1] + dp[1][1][i - 1] * 25 + dp[0][0][i - 1] + dp[0][1][i - 1] * (S[i] - 'a' - 1);
        }
    }
    cout << dp[1][1][N - 1].val << endl;
}
0