結果

問題 No.319 happy b1rthday 2 me
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2022-10-30 09:56:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 8,565 bytes
コンパイル時間 2,769 ms
コンパイル使用メモリ 200,556 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-07 03:41:35
合計ジャッジ時間 3,622 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2022.10.30 09:56:17
**/

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


struct Monoid {
    ll 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 Monoid2 {
    ll val;
    ll num1;
    ll num2;
    bool undef = true;
    Monoid2() { *this = zero(); }
    Monoid2(long long val, ll num1, ll num2, bool undef = true) : val(val),
                                                                  num1(num1),
                                                                  num2(num2),
                                                                  undef(undef) {}
    static Monoid2 zero() { return Monoid2(0,0,0); }
    static Monoid2 e() { return Monoid2(0,0,1,false); }
    Monoid2& operator+=(const Monoid2 &a) {
        if (this->undef) *this = a;
        else if (!a.undef) {
            this->val += a.val;
            this->num1 += a.num1;
            this->num2 += a.num2;
        }
        return *this;
    }
    Monoid2& operator*=(int c) {
        if (c == 2) this->val += this->num1;
        if (c == 1) this->num1 = this->num2;
        else this->num1 = 0;
        return *this;
    }
    friend Monoid2 operator+(const Monoid2& a, const Monoid2& b) {
        return Monoid2(a) += b;
    }
    friend Monoid2 operator*(const Monoid2& a, int c) {
        return Monoid2(a) *= c;
    }
    friend std::ostream& operator<<(std::ostream &os, const Monoid2 &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 AxBAutomaton : public Automaton {
private:
    int A,B;
    void set_init() { init = 0; }

    void set_delta() {
        int qsize = 4;
        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) {
                    if (c == 0) delta[state][c] = state;
                    else if (c == A) delta[state][c] = 1;
                    else delta[state][c] = 3;
                }
                else if (state <= 2) {
                    if (c == B) delta[state][c] = 2;
                    else delta[state][c] = 1;
                }
                else {
                    delta[state][c] = 3;
                }
            }
        }
    }

    void set_is_accept() {
        int qsize = 4;
        is_accept.resize(qsize,false);
        is_accept[2] = true;
    }

    void set_is_reject() {
        int qsize = 4;
        is_reject.resize(qsize,false);
        is_reject[3] = true;
    }
public:
    AxBAutomaton(int A, int B, int alpha_size = 10) : A(A),
                                                      B(B) {
        alphabet_size = alpha_size;
        set_init();
        set_delta();
        set_is_accept();
        set_is_reject();
    }
};

struct SimpleAutomaton : public Automaton {
private:
    void set_init() { init = 0; }

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

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

    void set_is_reject() {
        int qsize = 1;
        is_reject.resize(qsize);
        for (int state = 0; state < qsize; state++) {
            is_reject[state] = false;
        }
    }
public:
    SimpleAutomaton(int alpha_size = 10) {
        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;
}

template<typename Automaton>
Monoid2 digitDP2(const string &s, const Automaton &dfa, bool eq = 1) {
    vector<int> alpha(dfa.alphabet_size);
    iota(alpha.begin(), alpha.end(), 0);
    vector<vector<Monoid2>> dp(2,vector<Monoid2>(dfa.size(),Monoid2::zero()));
    dp[1][dfa.init] = Monoid2::e();
    for (int i = 0; i < s.size(); i++) {
        vector<vector<Monoid2>> dp2(2,vector<Monoid2>(dfa.size(),Monoid2::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;
    }
    Monoid2 ans = Monoid2::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;
    ll ans = 0;
    if (a == "1" && (b.size() > 1 || b[0] >='2')) ans++;
    if (b[0] == '2' && b.back() == '1') ans--;
    auto M1 = SimpleAutomaton();
    auto M2 = AxBAutomaton(2,1);
    ans += digitDP(b,M2).val+digitDP2(b,M1).val;
    ans -= digitDP(a,M2,false).val+digitDP2(a,M1,false).val;
    cout << ans << endl;
    return 0;
}
0