digitDP

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub kanpurin/digitDP

:warning: test/atcoder/ABC138_F.cpp

Depends on

Code

// "https://atcoder.jp/contests/abc138/tasks/abc138_f"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#include "digitDP/ADFA/digit_dp_adfa.hpp"
#include "digitDP/ADFA/geq_adfa.hpp"
#include "digitDP/ADFA/leq_adfa.hpp"
#include "digitDP/ProductofDFA/product_of_adfa.hpp"
#include "digitDP/ProductofDFA/same_msd.hpp"
#include "digitDP/ProductofDFA/leq_digits.hpp"
#include "digitDP/intersection.hpp"
#include "other/mint.hpp"

constexpr int MOD = 1e9 + 7;

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

string binarynumber(long long x, int len = -1) {
    string res;
    while(x) {
        if (x&1) res.push_back('1');
        else res.push_back('0');
        x>>=1;
    }
    while((int)res.size() < len) res.push_back('0');
    reverse(res.begin(), res.end());
    return res;
}

int main() {
    ll l,r;cin >> l >> r;
    string sl = binarynumber(l,64);
    string sr = binarynumber(r,64);
    auto M1 = GeqADFA(sl,true,2);
    auto M2 = LeqADFA(sr,true,2);
    auto M3 = ProductofADFA(M1,M2);
    auto M4 = SameMSDAutomaton(2);
    auto M5 = LeqDigitsAutomaton(2);
    auto M6 = IntersectionAutomaton(M4,M5);
    auto M7 = IntersectionAutomaton(M3,M6);
    cout << digitDP<Monoid>(M7) << endl;
    return 0;
}
#line 1 "test/atcoder/ABC138_F.cpp"
// "https://atcoder.jp/contests/abc138/tasks/abc138_f"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#line 3 "digitDP/automaton.hpp"

struct Automaton {
    std::vector<std::vector<int>> delta;
    std::vector<bool> is_accept;
    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 int size() const {return qsize; }
};
#line 4 "digitDP/ADFA/digit_dp_adfa.hpp"

// ADFAが受理する文字列すべてについて求める
template<typename Monoid>
Monoid digitDP(const Automaton &adfa) {
    std::vector<int> indeg(adfa.size());
    for (int i = 0; i < adfa.size(); i++) {
        for (int c = 0; c < adfa.alphabet_size; c++) {
            indeg[adfa.next(i,c)]++;
        }
    }
    std::vector<Monoid> dp(adfa.size());
    dp[adfa.init] = Monoid::e();
    Monoid ans;
    std::queue<int> que;
    que.push(adfa.init);
    while(!que.empty()) {
        int state = que.front(); que.pop();
        for (int c = 0; c < adfa.alphabet_size; c++) {
            int next_s = adfa.next(state,c);
            dp[next_s] += dp[state]*c;
            indeg[next_s]--;
            if (indeg[next_s] == 0) que.push(next_s);
        }
        if (adfa.accept(state)) ans += dp[state];
    }
    return ans;
}
#line 5 "digitDP/ADFA/geq_adfa.hpp"

// 辞書順s以上の長さ|s|の文字列を受理
// ADFA
struct GeqADFA : public Automaton {
private:
    std::string str;
    bool eq;

    void initializer() { 
        qsize = (str.size()+1)*2;
        init = 0;
        set_delta();
        set_is_accept();
    }

    void set_delta() {
        delta.resize(qsize,std::vector<int>(alphabet_size,0));
        for (int i = 0; i < str.size(); i++) {
            int state = i<<1;
            delta[state][str[i]-'0'] = state+2;
            for (int c = 0; c < str[i]-'0'; c++) {
                delta[state][c] = qsize-1;
            }
            for (int c = str[i]-'0'+1; c < alphabet_size; c++) {
                delta[state][c] = state+1;
            }
            for (int c = 0; c < alphabet_size; c++) {
                delta[state+1][c] = state+3;
            }
        }
        for (int c = 0; c < alphabet_size; c++) {
            delta[qsize-2][c] = qsize-1;
            delta[qsize-1][c] = qsize-1;
        }
    }

    void set_is_accept() {
        is_accept.resize(qsize,false);
        is_accept[qsize-2] = eq;
        is_accept[qsize-3] = true;
    }

public:
    GeqADFA(std::string s, bool eq = true, int alpha_size = 10) : str(s),
                                                                  eq(eq) {
        assert(s.size() >= 1);
        alphabet_size = alpha_size;
        initializer();
    }
};
#line 5 "digitDP/ADFA/leq_adfa.hpp"

// 辞書順s以下の長さ|s|の文字列を受理
// ADFA
struct LeqADFA : public Automaton {
private:
    std::string str;
    bool eq;

    void initializer() { 
        qsize = (str.size()+1)*2;
        init = 0;
        set_delta();
        set_is_accept();
    }

    void set_delta() {
        delta.resize(qsize,std::vector<int>(alphabet_size,0));
        for (int i = 0; i < (int)str.size(); i++) {
            int state = i<<1;
            delta[state][str[i]-'0'] = state+2;
            for (int c = 0; c < str[i]-'0'; c++) {
                delta[state][c] = state+1;
            }
            for (int c = str[i]-'0'+1; c < alphabet_size; c++) {
                delta[state][c] = qsize-1;
            }
            for (int c = 0; c < alphabet_size; c++) {
                delta[state+1][c] = state+3;
            }
        }
        for (int c = 0; c < alphabet_size; c++) {
            delta[qsize-2][c] = qsize-1;
            delta[qsize-1][c] = qsize-1;
        }
    }

    void set_is_accept() {
        is_accept.resize(qsize,false);
        is_accept[qsize-2] = eq;
        is_accept[qsize-3] = true;
    }

public:
    LeqADFA(std::string s, bool eq = true, int alpha_size = 10) : str(s),
                                                                  eq(eq) {
        assert(s.size() >= 1);
        alphabet_size = alpha_size;
        initializer();
    }
};
#line 4 "digitDP/ProductofDFA/product_of_adfa.hpp"

Automaton ProductofADFA(const Automaton &adfa, const Automaton &dfa) {
    Automaton M;
    M.alphabet_size = adfa.alphabet_size*dfa.alphabet_size;
    std::unordered_map<long long,int> table;
    std::vector<int> x = {adfa.init}, y = {dfa.init};
    table[(long long)x[0]*dfa.size()+y[0]] = 0;
    M.init = 0;
    for (int i = 0; i < x.size(); ++i) {
        M.delta.emplace_back(M.alphabet_size, -1);
        M.is_accept.emplace_back(adfa.accept(x[i]) && dfa.accept(y[i]));
        for (int c1 = 0; c1 < adfa.alphabet_size; c1++) {
            for (int c2 = 0; c2 < dfa.alphabet_size; c2++) {
                int c = c1*dfa.alphabet_size+c2;
                int u = adfa.next(x[i],c1), v = dfa.next(y[i],c2);
                long long ps = (long long)u*dfa.size()+v;
                if (table.find(ps) == table.end()) {
                    table[ps] = x.size();
                    x.emplace_back(u);
                    y.emplace_back(v);
                }
                M.delta[i][c] = table[ps];
            }
        }
    }
    M.qsize = M.delta.size();
    return M;
}
#line 3 "digitDP/ProductofDFA/same_msd.hpp"

// 最上位桁の数が一致する数字対(x,y)を受理
struct SameMSDAutomaton : public Automaton {
private:
    int alpha_size;
    void initializer() { 
        qsize = 3;
        init = 0;
        set_delta();
        set_is_accept();
    }

    void set_delta() {
        delta.resize(qsize,std::vector<int>(alphabet_size));
        for (int c1 = 0; c1 < alpha_size; c1++) {
            for (int c2 = 0; c2 < alpha_size; c2++) {
                int c = c1*alpha_size+c2;
                if (c1 == 0 && c2 == 0) delta[0][c] = 0;
                else if (c1 == c2) delta[0][c] = 1;
                else delta[0][c] = 2;
            }
        }
        for (int c = 0; c < alphabet_size; c++) {
            delta[1][c] = 1;
            delta[2][c] = 2;
        }
    }

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

public:
    SameMSDAutomaton(int alpha_size = 10) : alpha_size(alpha_size) {
        alphabet_size = alpha_size*alpha_size;
        initializer();
    }
};
#line 3 "digitDP/ProductofDFA/leq_digits.hpp"

// 全桁xi<=yiとなる文字列対(x,y)を受理
struct LeqDigitsAutomaton : public Automaton {
private:
    int alpha_size;
    void initializer() { 
        qsize = 2;
        init = 0;
        set_delta();
        set_is_accept();
    }

    void set_delta() {
        delta.resize(qsize,std::vector<int>(alphabet_size));
        for (int c1 = 0; c1 < alpha_size; c1++) {
            for (int c2 = 0; c2 < alpha_size; c2++) {
                int c = c1*alpha_size+c2;
                if (c1 <= c2) delta[0][c] = 0;
                else delta[0][c] = 1;
                delta[1][c] = 1;
            }
        }
    }

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

public:
    LeqDigitsAutomaton(int alpha_size = 10) : alpha_size(alpha_size) {
        alphabet_size = alpha_size*alpha_size;
        initializer();
    }
};
#line 3 "digitDP/intersection.hpp"

// どちらにも受理されるような文字列を受理
Automaton IntersectionAutomaton(const Automaton &A, const Automaton &B) {
    assert(A.alphabet_size == B.alphabet_size);
    Automaton M;
    M.alphabet_size = A.alphabet_size;
    std::vector<std::vector<int>> table(A.size(), std::vector<int>(B.size(),-1));
    std::vector<int> x = {A.init}, y = {B.init};
    table[x[0]][y[0]] = 0;
    M.init = 0;
    for (int i = 0; i < (int)x.size(); ++i) {
        M.delta.emplace_back(M.alphabet_size, -1);
        M.is_accept.emplace_back(A.accept(x[i]) && B.accept(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.emplace_back(u);
                y.emplace_back(v);
            }
            M.delta[i][c] = table[u][v];
        }
    }
    M.qsize = M.delta.size();
    return M;
}
#line 2 "other/mint.hpp"

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;
    }
    
    // 存在しない場合0を返す(二乗して確認).
    // O(log^2MOD)
    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;
    }
};
#line 14 "test/atcoder/ABC138_F.cpp"

constexpr int MOD = 1e9 + 7;

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

string binarynumber(long long x, int len = -1) {
    string res;
    while(x) {
        if (x&1) res.push_back('1');
        else res.push_back('0');
        x>>=1;
    }
    while((int)res.size() < len) res.push_back('0');
    reverse(res.begin(), res.end());
    return res;
}

int main() {
    ll l,r;cin >> l >> r;
    string sl = binarynumber(l,64);
    string sr = binarynumber(r,64);
    auto M1 = GeqADFA(sl,true,2);
    auto M2 = LeqADFA(sr,true,2);
    auto M3 = ProductofADFA(M1,M2);
    auto M4 = SameMSDAutomaton(2);
    auto M5 = LeqDigitsAutomaton(2);
    auto M6 = IntersectionAutomaton(M4,M5);
    auto M7 = IntersectionAutomaton(M3,M6);
    cout << digitDP<Monoid>(M7) << endl;
    return 0;
}
Back to top page