結果

問題 No.260 世界のなんとか3
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2022-10-30 08:02:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 7,325 bytes
コンパイル時間 2,489 ms
コンパイル使用メモリ 199,172 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-07 02:23:44
合計ジャッジ時間 4,115 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 61 ms
6,940 KB
testcase_29 AC 61 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2022.10.30 08:02:28
**/

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


struct Monoid {
    long long val;
    bool undef = true;
    Monoid() { *this = zero(); }
    Monoid(long long 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(); }
};

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 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<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 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();
    }
};

template<typename Automaton>
Monoid digitDP(const string &s, const Automaton &dfa, bool eq = 1) {
    vector<int> alpha(dfa.alphabet_size);
    iota(alpha.begin(), alpha.end(), 0);
    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 = 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;
}

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);
    cout << digitDP(b,M3).val-digitDP(a,M3,false).val-digitDP(b,M5).val+digitDP(a,M5,false).val << endl;
    return 0;
}
0