結果

問題 No.260 世界のなんとか3
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2022-10-31 03:02:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 12,544 bytes
コンパイル時間 2,762 ms
コンパイル使用メモリ 200,904 KB
実行使用メモリ 179,192 KB
最終ジャッジ日時 2024-07-07 18:34:06
合計ジャッジ時間 13,999 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 MLE -
testcase_04 MLE -
testcase_05 WA -
testcase_06 WA -
testcase_07 MLE -
testcase_08 MLE -
testcase_09 WA -
testcase_10 MLE -
testcase_11 MLE -
testcase_12 MLE -
testcase_13 WA -
testcase_14 MLE -
testcase_15 WA -
testcase_16 MLE -
testcase_17 MLE -
testcase_18 MLE -
testcase_19 MLE -
testcase_20 MLE -
testcase_21 MLE -
testcase_22 MLE -
testcase_23 WA -
testcase_24 MLE -
testcase_25 WA -
testcase_26 MLE -
testcase_27 AC 2 ms
5,376 KB
testcase_28 MLE -
testcase_29 MLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'Monoid digitDP(int, const Automaton&)':
main.cpp:231:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  231 |         for (auto [state,val] : dp) {
      |                   ^
main.cpp:240:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  240 |     for (auto [state,val] : dp)
      |               ^

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2022.10.31 03:02:30
**/

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


struct Monoid {
    ll val;
    bool undef = true;
    Monoid() { *this = zero(); }
    Monoid(ll 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 init;
    int alphabet_size = 10;
    int next(int state, int c) const { return delta[state][c]; }
    bool accept(int state) const { return is_accept[state]; }
    bool reject(int state) const { return is_reject[state]; }
    int size() const {return delta.size(); }
};

struct ZigZagAutomaton : public Automaton {
private:
    void set_init() { init = alphabet_size*3; }

    void set_delta() {
        int qsize = 2+alphabet_size*3;
        delta.resize(qsize,vector<int>(alphabet_size));
        for (int state = 0; state < qsize; state++) {
            for (int c = 0; c < alphabet_size; c++) {
                if (state == alphabet_size*3) {
                    if (c == 0)
                        delta[state][c] = alphabet_size*3;
                    else
                        delta[state][c] = c;
                }
                else if (state < alphabet_size) {
                    if (c < state)
                        delta[state][c] = alphabet_size+c;
                    else if (c > state)
                        delta[state][c] = alphabet_size*2+c;
                    else
                        delta[state][c] = alphabet_size*3+1;
                }
                else if (state < alphabet_size*2) {
                    if (c > state-alphabet_size)
                        delta[state][c] = alphabet_size*2+c;
                    else
                        delta[state][c] = alphabet_size*3+1;
                }
                else if (state < alphabet_size*3) {
                    if (c < state-alphabet_size*2)
                        delta[state][c] = alphabet_size+c;
                    else
                        delta[state][c] = alphabet_size*3+1;
                }
                else {
                    delta[state][c] = alphabet_size*3+1;
                }
            }
        }
    }

    void set_is_accept() {
        int qsize = size();
        is_accept.resize(qsize);
        for (int state = 0; state < qsize; state++) {
            is_accept[state] = 0 <= state && state < alphabet_size*3;
        }
    }

    void set_is_reject() {
        int qsize = size();
        is_reject.resize(qsize);
        is_reject[alphabet_size*3+1] = true;
    }
public:
    ZigZagAutomaton(int alpha_size = 10) {
        alphabet_size = alpha_size;
        set_init();
        set_delta();
        set_is_accept();
        set_is_reject();
    }
};

struct LengthAutomaton : public Automaton {
private:
    int len;
    void set_init() { init = 0; }

    void set_delta() {
        int qsize = len+1;
        delta.resize(qsize,vector<int>(alphabet_size));
        for (int c = 0; c < alphabet_size; c++) {
            if (c == 0) delta[init][c] = 0;
            else delta[init][c] = 1;
        }
        for (int state = 1; state < qsize-1; state++) {
            for (int c = 0; c < alphabet_size; c++) {
                delta[state][c] = state+1;
            }
        }
        for (int c = 0; c < alphabet_size; c++) {
            delta[len][c] = len;
        }
    }

    void set_is_accept() {
        int qsize = len+1;
        is_accept.resize(qsize,false);
        is_accept[len] = true;
    }

    void set_is_reject() {
        int qsize = len+1;
        is_reject.resize(qsize,false);
    }
public:
    LengthAutomaton(int len, int alpha_size = 10) :len(len) {
        alphabet_size = alpha_size;
        set_init();
        set_delta();
        set_is_accept();
        set_is_reject();
    }
};

template<class Automaton1, class Automaton2>
Automaton IntersectionAutomaton(const Automaton1 &A, const Automaton2 &B) {
    assert(A.alphabet_size == B.alphabet_size);
    Automaton M;
    M.alphabet_size = A.alphabet_size;
    vector<vector<int>> table(A.size(), vector<int>(B.size(),-1));
    vector<int> x = {A.init}, y = {B.init};
    table[x[0]][y[0]] = 0;
    M.init = 0;
    for (int i = 0; i < x.size(); ++i) {
        M.delta.push_back(vector<int>(M.alphabet_size, -1));
        M.is_accept.push_back(A.accept(x[i]) && B.accept(y[i]));
        M.is_reject.push_back(A.reject(x[i]) || B.reject(y[i]));
        for (int c = 0; c < A.alphabet_size; c++) {
            int u = A.next(x[i],c), v = B.next(y[i],c);
            if (table[u][v] == -1) {
                table[u][v] = x.size();
                x.push_back(u);
                y.push_back(v);
            }
            M.delta[i][c] = table[u][v];
        }
    }
    return M;
}

struct ModuloAutomaton : public Automaton {
private:
    int mod;
    void set_init() { init = 0; }

    void set_delta() {
        int qsize = mod;
        delta.resize(qsize,vector<int>(alphabet_size));
        for (int state = 0; state < qsize; state++) {
            for (int c = 0; c < alphabet_size; c++) {
                delta[state][c] = (state*10+c)%mod;
            }
        }
    }

    void set_is_accept() {
        int qsize = mod;
        is_accept.resize(qsize);
        for (int state = 0; state < qsize; state++) {
            is_accept[state] = state == 0;
        }
    }

    void set_is_reject() {
        int qsize = mod;
        is_reject.resize(qsize);
        for (int state = 0; state < qsize; state++) {
            is_reject[state] = false;
        }
    }
public:
    ModuloAutomaton(int mod, int alpha_size = 10) : mod(mod) {
        alphabet_size = alpha_size;
        set_init();
        set_delta();
        set_is_accept();
        set_is_reject();
    }
};

template<typename Automaton>
Monoid digitDP(int n, const Automaton &dfa) {
    unordered_map<int,Monoid> dp,dp2;
    dp[dfa.init] = Monoid::e();
    Monoid ans = Monoid::zero();
    vector<int> alpha(dfa.alphabet_size);
    iota(alpha.begin(), alpha.end(), 0);
    for (int i = 0; i < n; i++) {
        for (auto [state,val] : dp) {
            for (int c : alpha) {
                auto n_state = dfa.next(state,c);
                if (dfa.reject(n_state)) continue;
                dp2[n_state] += dp[state]*c;
            }
        }
        dp.swap(dp2); dp2.clear();
    }
    for (auto [state,val] : dp)
        if (dfa.accept(state)) ans += val;
    return ans.val;
}

template<class Automaton1, class Automaton2>
Automaton UnionAutomaton(const Automaton1 &A, const Automaton2 &B) {
    assert(A.alphabet_size == B.alphabet_size);
    Automaton M;
    M.alphabet_size = A.alphabet_size;
    vector<vector<int>> table(A.size(), vector<int>(B.size(),-1));
    vector<int> x = {A.init}, y = {B.init};
    table[x[0]][y[0]] = 0;
    M.init = 0;
    for (int i = 0; i < x.size(); ++i) {
        M.delta.push_back(vector<int>(M.alphabet_size, -1));
        M.is_accept.push_back(A.accept(x[i]) || B.accept(y[i]));
        M.is_reject.push_back(A.reject(x[i]) && B.reject(y[i]));
        for (int c = 0; c < A.alphabet_size; c++) {
            int u = A.next(x[i],c), v = B.next(y[i],c);
            if (table[u][v] == -1) {
                table[u][v] = x.size();
                x.push_back(u);
                y.push_back(v);
            }
            M.delta[i][c] = table[u][v];
        }
    }
    return M;
}

struct IncludeAllAutomaton : public Automaton {
private:
    void set_init() { init = (1<<(int)elems.size()); }

    void set_delta() {
        int qsize = 1+(1<<(int)elems.size());
        delta.resize(qsize,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 {
                    delta[state][c] = state==init?0:state;
                    for (int i = 0; i < elems.size(); i++) {
                        if (c == elems[i]) {
                            delta[state][c] = delta[state][c]|1<<i;
                            break;
                        }
                    }
                }
            }
        }
    }

    void set_is_accept() {
        int qsize = 1+(1<<(int)elems.size());
        is_accept.resize(qsize);
        for (int state = 0; state < qsize; state++) {
            is_accept[state] = state == (1<<(int)elems.size())-1;
        }
    }

    void set_is_reject() {
        int qsize = 1+(1<<(int)elems.size());
        is_reject.resize(qsize);
        for (int state = 0; state < qsize; state++) {
            is_reject[state] = false;
        }
    }
public:
    vector<int> elems;

    IncludeAllAutomaton(vector<int> elems, int alpha_size = 10) : elems(elems) {
        alphabet_size = alpha_size;
        set_init();
        set_delta();
        set_is_accept();
        set_is_reject();
    }
};


struct LeqAutomaton : public Automaton {
private:
    string str;
    bool eq;

    inline int _idx(int state) { return state/3; }
    inline int _comp(int state) { return state%3; }
    inline int tostate(int idx, int comp) { return idx*3+comp; }

    void set_init() { init = tostate(0,0); }

    void set_delta() {
        int qsize = (str.size()+1)*3;
        delta.resize(qsize,vector<int>(alphabet_size));
        for (int state = 0; state < qsize; state++) {
            int idx = _idx(state);
            int comp = _comp(state);
            if (idx < str.size()) {
                if (comp == 0) {
                    for (int c = 0; c < str[idx]-'0'; c++) 
                        delta[state][c] = tostate(idx+1,1);
                    for (int c = str[idx]-'0'+1; c < alphabet_size; c++)
                        delta[state][c] = tostate(idx+1,2);
                    delta[state][str[idx]-'0'] = tostate(idx+1,0);
                }
                else if (comp == 1) {
                    for (int c = 0; c < alphabet_size; c++)
                        delta[state][c] = tostate(idx+1,1);
                }
                else if (comp == 2) {
                    for (int c = 0; c < alphabet_size; c++)
                        delta[state][c] = tostate(idx+1,2);
                }
            }
            else {
                for (int c = 0; c < alphabet_size; c++)
                    delta[state][c] = tostate(str.size(),2);
            }
        }
    }

    void set_is_accept() {
        int qsize = (str.size()+1)*3;
        is_accept.resize(qsize);
        for (int state = 0; state < qsize; state++) {
            if (eq) is_accept[state] = _idx(state) == str.size() && _comp(state) != 2;
            else is_accept[state] = _idx(state) == str.size() && _comp(state) == 1;
        }
    }

    void set_is_reject() {
        int qsize = (str.size()+1)*3;
        is_reject.resize(qsize);
        for (int state = 0; state < qsize; state++) {
            is_reject[state] = _comp(state) == 2;
        }
    }
public:
    LeqAutomaton(string s, bool eq = true, int alpha_size = 10) : str(s),
                                                                  eq(eq) {
        alphabet_size = alpha_size;
        set_init();
        set_delta();
        set_is_accept();
        set_is_reject();
    }
};


int main() {
    string a,b;cin >> a >> b;
    auto M1 = ModuloAutomaton(3);
    auto M2 = IncludeAllAutomaton({3});
    auto M3 = UnionAutomaton(M1,M2);
    auto M4 = ModuloAutomaton(8);
    auto M5 = IntersectionAutomaton(M3,M4);
    auto M6 = LeqAutomaton(b); 
    auto M7 = LeqAutomaton(a,false); 
    auto M8 = IntersectionAutomaton(M3,M6); 
    auto M9 = IntersectionAutomaton(M3,M7);
    auto M10 = IntersectionAutomaton(M5,M6);
    auto M11 = IntersectionAutomaton(M5,M7);

    cout << digitDP(b.size(),M8).val-digitDP(a.size(),M9).val-digitDP(b.size(),M10).val+digitDP(a.size(),M11).val << endl;
    return 0;
}
0