/** * @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> delta; vector 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 Automaton UnionAutomaton(const Automaton1 &A, const Automaton2 &B) { assert(A.alphabet_size == B.alphabet_size); Automaton M; M.alphabet_size = A.alphabet_size; vector> table(A.size(), vector(B.size(),-1)); vector 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(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(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(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 Monoid digitDP(const string &s, const Automaton &dfa, bool eq = 1) { vector alpha(dfa.alphabet_size); iota(alpha.begin(), alpha.end(), 0); vector> dp(2,vector(dfa.size(),Monoid::zero())); dp[1][dfa.init] = Monoid::e(); for (int i = 0; i < s.size(); i++) { vector> dp2(2,vector(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 Monoid2 digitDP2(const string &s, const Automaton &dfa, bool eq = 1) { vector alpha(dfa.alphabet_size); iota(alpha.begin(), alpha.end(), 0); vector> dp(2,vector(dfa.size(),Monoid2::zero())); dp[1][dfa.init] = Monoid2::e(); for (int i = 0; i < s.size(); i++) { vector> dp2(2,vector(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; }