結果

問題 No.685 Logical Operations
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2022-11-06 11:42:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 11,314 bytes
コンパイル時間 3,259 ms
コンパイル使用メモリ 236,012 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-27 07:57:14
合計ジャッジ時間 4,590 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2022.11.06 11:42:45
**/

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


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

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

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

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

struct ForbiddenNumberAutomaton : public Automaton {
private:
    std::vector<bool> banflg;
    
    void initializer() { 
        qsize = 3;
        init = 0;
        set_delta();
        set_is_accept();
    }

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

    void set_is_accept() {
        is_accept.resize(qsize,false);
        is_accept[1] = true;
    }
public:
    ForbiddenNumberAutomaton(std::vector<bool> banflg, int alpha_size = 10) : banflg(banflg) {
        assert(banflg.size() == alpha_size);
        alphabet_size = alpha_size;
        initializer();
    }
};

Automaton PairADFA(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;
}

struct SameMSDPairAutomaton : 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:
    SameMSDPairAutomaton(int alpha_size = 10) : alpha_size(alpha_size) {
        alphabet_size = alpha_size*alpha_size;
        initializer();
    }
};

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

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

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 n;cin >> n;
    if (n == 0) {
        puts("0");
        return 0;
    }
    string ns = binarynumber(n);
    auto M1 = ForbiddenNumberAutomaton({0,0,0,1},4);
    for (int i = 0; i < M1.size(); i++) {
        M1.is_accept[i] = M1.is_accept[i] ^ true;
    }
    auto M2 = SameMSDPairAutomaton(2);
    for (int i = 0; i < M2.size(); i++) {
        M2.is_accept[i] = M2.is_accept[i] ^ true;
    }
    auto M3 = LeqADFA(ns,true,2);
    auto M4 = PairADFA(M3,M3);
    auto M5 = IntersectionAutomaton(M1,M2);
    auto M6 = IntersectionAutomaton(M4,M5);
    cout << digitDP<Monoid>(M6).val/2 << endl;
    return 0;
}
0