結果

問題 No.1269 I hate Fibonacci Number
ユーザー kimiyukikimiyuki
提出日時 2020-10-24 00:01:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 34 ms / 3,000 ms
コード長 7,400 bytes
コンパイル時間 2,721 ms
コンパイル使用メモリ 226,236 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-21 14:02:43
合計ジャッジ時間 4,368 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#line 1 "main.cpp"
#include <bits/stdc++.h>
#line 2 "/home/user/Library/utils/macros.hpp"
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
#line 4 "/home/user/Library/modulus/modpow.hpp"

inline int32_t modpow(uint_fast64_t x, uint64_t k, int32_t MOD) {
    assert (/* 0 <= x and */ x < (uint_fast64_t)MOD);
    uint_fast64_t y = 1;
    for (; k; k >>= 1) {
        if (k & 1) (y *= x) %= MOD;
        (x *= x) %= MOD;
    }
    assert (/* 0 <= y and */ y < (uint_fast64_t)MOD);
    return y;
}
#line 5 "/home/user/Library/modulus/modinv.hpp"

inline int32_t modinv_nocheck(int32_t value, int32_t MOD) {
    assert (0 <= value and value < MOD);
    if (value == 0) return -1;
    int64_t a = value, b = MOD;
    int64_t x = 0, y = 1;
    for (int64_t u = 1, v = 0; a; ) {
        int64_t q = b / a;
        x -= q * u; std::swap(x, u);
        y -= q * v; std::swap(y, v);
        b -= q * a; std::swap(b, a);
    }
    if (not (value * x + MOD * y == b and b == 1)) return -1;
    if (x < 0) x += MOD;
    assert (0 <= x and x < MOD);
    return x;
}

inline int32_t modinv(int32_t x, int32_t MOD) {
    int32_t y = modinv_nocheck(x, MOD);
    assert (y != -1);
    return y;
}
#line 6 "/home/user/Library/modulus/mint.hpp"

/**
 * @brief quotient ring / 剰余環 $\mathbb{Z}/n\mathbb{Z}$
 */
template <int32_t MOD>
struct mint {
    int32_t value;
    mint() : value() {}
    mint(int64_t value_) : value(value_ < 0 ? value_ % MOD + MOD : value_ >= MOD ? value_ % MOD : value_) {}
    mint(int32_t value_, std::nullptr_t) : value(value_) {}
    explicit operator bool() const { return value; }
    inline mint<MOD> operator + (mint<MOD> other) const { return mint<MOD>(*this) += other; }
    inline mint<MOD> operator - (mint<MOD> other) const { return mint<MOD>(*this) -= other; }
    inline mint<MOD> operator * (mint<MOD> other) const { return mint<MOD>(*this) *= other; }
    inline mint<MOD> & operator += (mint<MOD> other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; }
    inline mint<MOD> & operator -= (mint<MOD> other) { this->value -= other.value; if (this->value <    0) this->value += MOD; return *this; }
    inline mint<MOD> & operator *= (mint<MOD> other) { this->value = (uint_fast64_t)this->value * other.value % MOD; return *this; }
    inline mint<MOD> operator - () const { return mint<MOD>(this->value ? MOD - this->value : 0, nullptr); }
    inline bool operator == (mint<MOD> other) const { return value == other.value; }
    inline bool operator != (mint<MOD> other) const { return value != other.value; }
    inline mint<MOD> pow(uint64_t k) const { return mint<MOD>(modpow(value, k, MOD), nullptr); }
    inline mint<MOD> inv() const { return mint<MOD>(modinv(value, MOD), nullptr); }
    inline mint<MOD> operator / (mint<MOD> other) const { return *this * other.inv(); }
    inline mint<MOD> & operator /= (mint<MOD> other) { return *this *= other.inv(); }
};
template <int32_t MOD> mint<MOD> operator + (int64_t value, mint<MOD> n) { return mint<MOD>(value) + n; }
template <int32_t MOD> mint<MOD> operator - (int64_t value, mint<MOD> n) { return mint<MOD>(value) - n; }
template <int32_t MOD> mint<MOD> operator * (int64_t value, mint<MOD> n) { return mint<MOD>(value) * n; }
template <int32_t MOD> mint<MOD> operator / (int64_t value, mint<MOD> n) { return mint<MOD>(value) / n; }
template <int32_t MOD> std::istream & operator >> (std::istream & in, mint<MOD> & n) { int64_t value; in >> value; n = value; return in; }
template <int32_t MOD> std::ostream & operator << (std::ostream & out, mint<MOD> n) { return out << n.value; }
#line 4 "main.cpp"
using namespace std;

// $ ghci
// Prelude> let fib = [1, 1] ++ zipWith (+) fib (tail fib) in takeWhile (<= 10 ^ 18) fib
int64_t fib[] = {1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309,3524578,5702887,9227465,14930352,24157817,39088169,63245986,102334155,165580141,267914296,433494437,701408733,1134903170,1836311903,2971215073,4807526976,7778742049,12586269025,20365011074,32951280099,53316291173,86267571272,139583862445,225851433717,365435296162,591286729879,956722026041,1548008755920,2504730781961,4052739537881,6557470319842,10610209857723,17167680177565,27777890035288,44945570212853,72723460248141,117669030460994,190392490709135,308061521170129,498454011879264,806515533049393,1304969544928657,2111485077978050,3416454622906707,5527939700884757,8944394323791464,14472334024676221,23416728348467685,37889062373143906,61305790721611591,99194853094755497,160500643816367088,259695496911122585,420196140727489673,679891637638612258};

constexpr int64_t MOD = 1000000007;
mint<MOD> solve(int n, int64_t l, int64_t r) {
    vector<int64_t> forbidden;
    for (int64_t fib_i : fib) {
        if (l <= fib_i and fib_i < r) {
            forbidden.push_back(fib_i);
        }
    }

    vector<string> patterns;
    for (int64_t f : forbidden) {
        string s;
        while (f) {
            s.push_back('0' + f % 10);
            f /= 10;
        }
        reverse(ALL(s));
        bool found = false;
        for (const string& pat : patterns) {
            if (pat.find(s) != string::npos) {
                found = true;
            }
        }
        if (not found) {
            patterns.push_back(s);
        }
    }
    // cerr << "patterns.size() = " << patterns.size() << endl;

    auto check = [&](const string& s) {
        for (const string& pat : patterns) {
            if (s.find(pat) != string::npos) {
                return false;
            }
        }
        return true;
    };

    auto simplify = [&](string s) {
        while (not s.empty()) {
            for (const string& pat : patterns) {
                if (pat.find(s) == 0) {
                    return s;
                }
            }
            s = s.substr(1);
        }
        return s;
    };

    unordered_map<string, int> words;
    vector<vector<int> > g;
    auto go = [&](auto&& go, string s) {
        if (words.count(s)) return;
        words.emplace(s, words.size());
        g.emplace_back();
        REP3 (c, '0', '9' + 1) {
            string t = s + string(1, c);
            if (not check(t)) continue;
            t = simplify(t);
            go(go, t);
            g[words[s]].push_back(words[t]);
        }
    };
    go(go, "");

    // dp
    vector<mint<MOD> > cur(words.size());
    cur[0] += 1;
    REP (iteration, n) {
        // cerr << "iteration = " << iteration << ": cur.size() = " << cur.size() << endl;
        vector<mint<MOD> > prv(words.size());
        cur.swap(prv);
        REP (i, words.size()) {
            for (int j : g[i]) {
                cur[j] += prv[i];
            }
        }
    }

    mint<MOD> ans = 0;
    REP (i, words.size()) {
        ans += cur[i];
    }
    ans -= 1;  // for 0
    return ans;
}

// generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator)
int main() {
    int N;
    int64_t L, R;
    cin >> N >> L >> R;
    ++ R;
    auto ans = solve(N, L, R);
    cout << ans << endl;
    return 0;
}
0