結果

問題 No.1417 100の倍数かつ正整数(2)
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2022-10-30 11:20:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 3,000 ms
コード長 10,657 bytes
コンパイル時間 2,091 ms
コンパイル使用メモリ 200,268 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-07 04:29:47
合計ジャッジ時間 3,246 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2022.10.30 11:20: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 {
    mint<MOD> 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 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 ProdOfDigitsAutomaton : public Automaton {
private:
    long long N;
    int cnt_2 = 0,cnt_3 = 0,cnt_5 = 0,cnt_7 = 0;

    inline int _tostate(int i2, int i3, int i5, int i7) {
        return min(i2,cnt_2)+(min(i3,cnt_3)+(min(i5,cnt_5)+min(i7,cnt_7)*(cnt_5+1))*(cnt_3+1))*(cnt_2+1);
    }

    int _nextstate(int i2, int i3, int i5, int i7, int c) {
        if (c == 0) return _tostate(cnt_2,cnt_3,cnt_5,cnt_7);
        else if (c == 1) return _tostate(i2,i3,i5,i7);
        else if (c == 2) return _tostate(i2+1,i3,i5,i7);
        else if (c == 3) return _tostate(i2,i3+1,i5,i7);
        else if (c == 4) return _tostate(i2+2,i3,i5,i7);
        else if (c == 5) return _tostate(i2,i3,i5+1,i7);
        else if (c == 6) return _tostate(i2+1,i3+1,i5,i7);
        else if (c == 7) return _tostate(i2,i3,i5,i7+1);
        else if (c == 8) return _tostate(i2+3,i3,i5,i7);
        else return _tostate(i2,i3+2,i5,i7);
    }

    void set_init() { 
        long long M = N;
        while(M%2 == 0) M/=2,cnt_2++;
        while(M%3 == 0) M/=3,cnt_3++;
        while(M%5 == 0) M/=5,cnt_5++;
        while(M%7 == 0) M/=7,cnt_7++;
        assert(M == 1);
        init = (cnt_2+1)*(cnt_3+1)*(cnt_5+1)*(cnt_7+1); 
    }

    void set_delta() {
        int qsize = (cnt_2+1)*(cnt_3+1)*(cnt_5+1)*(cnt_7+1)+1;
        delta.resize(qsize,vector<int>(alphabet_size));
        for (int c = 0; c < alphabet_size; c++) {
            if (c == 0) delta[init][c] = init;
            else delta[init][c] = _nextstate(0,0,0,0,c);
        }
        for (int i2 = 0; i2 <= cnt_2; i2++) {
            for (int i3 = 0; i3 <= cnt_3; i3++) {
                for (int i5 = 0; i5 <= cnt_5; i5++) {
                    for (int i7 = 0; i7 <= cnt_7; i7++) {
                        int state = _tostate(i2,i3,i5,i7);
                        for (int c = 0; c < alphabet_size; c++) {
                            delta[state][c] = _nextstate(i2,i3,i5,i7,c);
                        }
                    }
                }
            }
        }
    }

    void set_is_accept() {
        int qsize = (cnt_2+1)*(cnt_3+1)*(cnt_5+1)*(cnt_7+1)+1;
        is_accept.resize(qsize);
        is_accept[_tostate(cnt_2,cnt_3,cnt_5,cnt_7)] = true;
    }

    void set_is_reject() {
        int qsize = (cnt_2+1)*(cnt_3+1)*(cnt_5+1)*(cnt_7+1)+1;
        is_reject.resize(qsize,false);
    }
public:
    ProdOfDigitsAutomaton(long long N, int alpha_size = 10) : N(N) {
        assert(alpha_size == 10);
        alphabet_size = alpha_size;
        set_init();
        set_delta();
        set_is_accept();
        set_is_reject();
    }
};

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

    void set_delta() {
        int qsize = 3;
        delta.resize(qsize,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() {
        int qsize = 3;
        is_accept.resize(qsize);
        for (int state = 0; state < qsize; state++) {
            is_accept[state] = state == 1;
        }
    }

    void set_is_reject() {
        int qsize = 3;
        is_reject.resize(qsize);
        for (int state = 0; state < qsize; state++) {
            is_reject[state] = state == 2;
        }
    }
public:
    vector<bool> banflg;

    ForbiddenNumberAutomaton(vector<bool> banflg, int alpha_size = 10) : banflg(banflg) {
        assert(banflg.size() == alpha_size);
        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 n;cin >> n;
    auto M1 = ForbiddenNumberAutomaton({1,0,0,0,0,0,0,0,0,0});
    auto M2 = ProdOfDigitsAutomaton(100);
    auto M3 = IntersectionAutomaton(M1,M2);
    cout << digitDP(n,M3) << endl;
    return 0;
}
0