結果

問題 No.1740 Alone 'a'
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2022-11-03 08:41:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 183 ms / 2,000 ms
コード長 7,291 bytes
コンパイル時間 2,106 ms
コンパイル使用メモリ 188,000 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-17 19:18:42
合計ジャッジ時間 6,415 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 178 ms
6,940 KB
testcase_04 AC 183 ms
6,940 KB
testcase_05 AC 8 ms
6,944 KB
testcase_06 AC 7 ms
6,944 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 30 ms
6,944 KB
testcase_12 AC 130 ms
6,940 KB
testcase_13 AC 124 ms
6,940 KB
testcase_14 AC 152 ms
6,944 KB
testcase_15 AC 3 ms
6,944 KB
testcase_16 AC 114 ms
6,940 KB
testcase_17 AC 3 ms
6,940 KB
testcase_18 AC 21 ms
6,940 KB
testcase_19 AC 129 ms
6,940 KB
testcase_20 AC 68 ms
6,940 KB
testcase_21 AC 95 ms
6,940 KB
testcase_22 AC 110 ms
6,940 KB
testcase_23 AC 113 ms
6,940 KB
testcase_24 AC 68 ms
6,940 KB
testcase_25 AC 70 ms
6,944 KB
testcase_26 AC 89 ms
6,940 KB
testcase_27 AC 96 ms
6,940 KB
testcase_28 AC 80 ms
6,944 KB
testcase_29 AC 155 ms
6,940 KB
testcase_30 AC 99 ms
6,944 KB
testcase_31 AC 118 ms
6,940 KB
testcase_32 AC 84 ms
6,940 KB
testcase_33 AC 115 ms
6,940 KB
testcase_34 AC 26 ms
6,940 KB
testcase_35 AC 81 ms
6,944 KB
testcase_36 AC 65 ms
6,940 KB
testcase_37 AC 26 ms
6,940 KB
testcase_38 AC 54 ms
6,940 KB
testcase_39 AC 11 ms
6,944 KB
testcase_40 AC 35 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2022.11.03 08:41:16
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;


template< int MOD >
struct mint {
public:
    unsigned int x;
    mint() : x(0) {}
    mint(long long v) {
        long long w = (long long)(v % (long long)(MOD));
        if (w < 0) w += MOD;
        x = (unsigned int)(w);
    }
    mint(std::string &s) {
        unsigned int z = 0;
        for (int i = 0; i < s.size(); i++) {
            z *= 10;
            z += s[i] - '0';
            z %= MOD;
        }
        x = z;
    }
    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }
    mint& operator+=(const mint &a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint &a) {
        if ((x -= a.x) >= MOD) x += MOD;
        return *this;
    }
    mint& operator*=(const mint &a) {
        unsigned long long z = x;
        z *= a.x;
        x = (unsigned int)(z % MOD);
        return *this;
    }
    mint& operator/=(const mint &a) {return *this = *this * a.inv(); }
    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs.x == rhs.x;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs.x != rhs.x;
    }
    friend std::ostream& operator<<(std::ostream &os, const mint &n) {
        return os << n.x;
    }
    friend std::istream &operator>>(std::istream &is, mint &n) {
        unsigned int x;
        is >> x;
        n = mint(x);
        return is;
    }
    mint inv() const {
        assert(x);
        return pow(MOD-2);
    }
    mint pow(long long n) const {        
        assert(0 <= n);
        mint p = *this, r = 1;
        while (n) {
            if (n & 1) r *= p;
            p *= p;
            n >>= 1;
        }
        return r;
    }
    
    mint sqrt() const {
        if (this->x < 2) return *this;
        if (this->pow((MOD-1)>>1).x != 1) return mint(0);
        mint b = 1, one = 1;
        while (b.pow((MOD-1) >> 1) == 1) b += one;
        long long m = MOD-1, e = 0;
        while (m % 2 == 0) m >>= 1, e += 1;
        mint x = this->pow((m - 1) >> 1);
        mint y = (*this) * x * x;
        x *= (*this);
        mint z = b.pow(m);
        while (y.x != 1) {
            int j = 0;
            mint t = y;
            while (t != one) j += 1, t *= t;
            z = z.pow(1LL << (e-j-1));
            x *= z; z *= z; y *= z; e = j;
        }
        return x;
    }
};

constexpr int MOD = 998244353;

struct Monoid {
    using T = mint<MOD>;
    T val;
    bool undef = true;
    Monoid() { *this = zero(); }
    Monoid(T val, bool undef = true) : val(val),
                                       undef(undef) {}
    static Monoid zero() { return Monoid(0); }
    static Monoid e() { return Monoid(1,false); }
    Monoid& operator+=(const Monoid &a) {
        if (this->undef) *this = a;
        else if (!a.undef) this->val += a.val;
        return *this;
    }
    Monoid& operator*=(int c) {
        return *this;
    }
    friend Monoid operator+(const Monoid& a, const Monoid& b) {
        return Monoid(a) += b;
    }
    friend Monoid operator*(const Monoid& a, int c) {
        return Monoid(a) *= c;
    }
    friend std::ostream& operator<<(std::ostream &os, const Monoid &x) {
        return os << x.val;
    }
};

struct Automaton {
    vector<vector<int>> delta;
    vector<bool> is_accept, is_reject;
    int qsize;
    int init;
    int alphabet_size = 10;
    inline int next(int state, int c) const { return delta[state][c]; }
    inline bool accept(int state) const { return is_accept[state]; }
    inline bool reject(int state) const { return is_reject[state]; }
    inline int size() const {return qsize; }
};

template<typename Automaton>
Monoid digitDP(const string &s, const Automaton &dfa, bool eq = 1) {
    vector<vector<Monoid>> dp(2,vector<Monoid>(dfa.size(),Monoid::zero()));
    dp[1][dfa.init] = Monoid::e();
    for (int i = 0; i < s.size(); i++) {
        vector<vector<Monoid>> dp2(2,vector<Monoid>(dfa.size(),Monoid::zero()));
        for (int tight = 0; tight <= 1; tight++) {
            for (int state = 0; state < dfa.size(); state++) {
                if (dfa.reject(state) || dp[tight][state].undef) continue;
                int lim = (tight ? s[i] - '0' : dfa.alphabet_size - 1);
                for (int c = 0; c <= lim; c++) {
                    int tight_ = tight && c == lim;
                    int state_ = dfa.next(state,c);
                    if (dfa.reject(state_)) continue;
                    dp2[tight_][state_] += dp[tight][state]*c;
                }
            }
        }
        dp = move(dp2);
    }
    Monoid ans = Monoid::zero();
    for (int tight = 0; tight <= eq; tight++)
        for (int state = 0; state < dfa.size(); state++)
            if (dfa.accept(state)) ans += dp[tight][state];
    return ans;
}

struct CountNumberAutomaton : public Automaton {
private:
    std::vector<bool> flg;
    int num;
    bool eq;

    void initializer() { 
        assert(flg.size() == alphabet_size);
        qsize = num+3;
        init = num+2;
        set_delta();
        set_is_accept();
        set_is_reject();
    }

    void set_delta() {
        delta.resize(qsize,std::vector<int>(alphabet_size));
        for (int state = 0; state < qsize; state++) {
            for (int c = 0; c < alphabet_size; c++) {
                if (state == init && c == 0) delta[state][c] = init;
                else if (state == init) delta[state][c] = flg[c]?1:0;
                else if (state == num+1) delta[state][c] = state;
                else delta[state][c] = flg[c]?state+1:state;
            }
        }
    }

    void set_is_accept() {
        is_accept.resize(qsize,false);
        if (eq) is_accept[num] = true;
        else {
            for (int state = 0; state <= num; state++) {
                is_accept[state] = true;
            }
            is_accept[num+2] = true;
        }
    }

    void set_is_reject() {
        is_reject.resize(qsize,false);
        is_reject[num+1] = true;
    }
public:
    CountNumberAutomaton(std::vector<bool> flg, int num, bool eq = false, int alpha_size = 10) : flg(flg),
                                                                                                 num(num),
                                                                                                 eq(eq) {
        alphabet_size = alpha_size;
        initializer();
    }
};
 
int main() {
    int n;cin >> n;
    string s;cin >> s;
    for (int i = 0; i < n; i++) {
        s[i] = s[i]-'a'+'0';
    }
    s = "1"+s;
    string t(n+1,'0');
    t[0] = '1';
    vector<bool> flg(26,false); flg[0] = true;
    auto M = CountNumberAutomaton(flg,1,true,26);
    cout << digitDP(s,M,false).val-digitDP(t,M,false).val << endl;
    return 0;
}
0