結果

問題 No.1269 I hate Fibonacci Number
ユーザー hitonanodehitonanode
提出日時 2020-10-24 19:13:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 3,000 ms
コード長 5,275 bytes
コンパイル時間 2,930 ms
コンパイル使用メモリ 231,748 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-21 15:29:25
合計ジャッジ時間 4,417 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 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 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 17 ms
5,376 KB
testcase_14 AC 22 ms
5,376 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 17 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 19 ms
5,376 KB
testcase_19 AC 14 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 12 ms
5,376 KB
testcase_22 AC 11 ms
5,376 KB
testcase_23 AC 12 ms
5,376 KB
testcase_24 AC 6 ms
5,376 KB
testcase_25 AC 8 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 5 ms
5,376 KB
testcase_29 AC 9 ms
5,376 KB
testcase_30 AC 4 ms
5,376 KB
testcase_31 AC 27 ms
5,376 KB
testcase_32 AC 8 ms
5,376 KB
testcase_33 AC 55 ms
5,376 KB
testcase_34 AC 3 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 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

// Aho-Corasick algorithm
// Complexity:
// - add_keyword(): O(|str|)
// - build_aho_corasick(): O(\sum_i |str_i|)
template <typename T>
struct Trie {
    std::vector<std::map<char, int>> child;
    std::vector<int> fail;
    std::vector<T> node_info;
    int root;
    Trie() : child(1), fail(1), node_info(1), root(0) {}
    void add_keyword(const std::string &str, T info)
    {
        int now = 0;
        for (auto c : str)
        {
            if (child[now].count(c) == 0)
            {
                child[now][c] = fail.size();
                child.emplace_back();
                fail.emplace_back(root);
                node_info.emplace_back(0);
            }
            now = child[now][c];
        }
        node_info[now] += info;
    }

    void build_aho_corasick()
    {
        std::queue<int> q;
        for (auto pa : child[root]) q.push(pa.second);
        while (!q.empty())
        {
            int s = q.front();
            q.pop();
            for (auto pa : child[s])
            {
                int t = fail[s];
                while (t and child[t].count(pa.first) == 0) t = fail[t];
                int u = (child[t].count(pa.first) ? child[t][pa.first] : root);
                fail[pa.second] = u;
                node_info[pa.second] += node_info[u];
                q.push(pa.second);
            }
        }
    }
};


using namespace std;
using lint = long long;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)


template <int mod>
struct ModInt
{
    using lint = long long;
    int val;
    constexpr ModInt() : val(0) {}
    constexpr ModInt &_setval(lint v) { val = (v >= mod ? v - mod : v); return *this; }
    constexpr ModInt(lint v) { _setval(v % mod + mod); }
    explicit operator bool() const { return val != 0; }
    constexpr ModInt operator+(const ModInt &x) const { return ModInt()._setval((lint)val + x.val); }
    constexpr ModInt operator-(const ModInt &x) const { return ModInt()._setval((lint)val - x.val + mod); }
    constexpr ModInt operator*(const ModInt &x) const { return ModInt()._setval((lint)val * x.val % mod); }
    constexpr ModInt operator/(const ModInt &x) const { return ModInt()._setval((lint)val * x.inv() % mod); }
    constexpr ModInt operator-() const { return ModInt()._setval(mod - val); }
    constexpr ModInt &operator+=(const ModInt &x) { return *this = *this + x; }
    constexpr ModInt &operator-=(const ModInt &x) { return *this = *this - x; }
    constexpr ModInt &operator*=(const ModInt &x) { return *this = *this * x; }
    constexpr ModInt &operator/=(const ModInt &x) { return *this = *this / x; }
    friend constexpr ModInt operator+(lint a, const ModInt &x) { return ModInt()._setval(a % mod + x.val); }
    friend constexpr ModInt operator-(lint a, const ModInt &x) { return ModInt()._setval(a % mod - x.val + mod); }
    friend constexpr ModInt operator*(lint a, const ModInt &x) { return ModInt()._setval(a % mod * x.val % mod); }
    friend constexpr ModInt operator/(lint a, const ModInt &x) { return ModInt()._setval(a % mod * x.inv() % mod); }
    constexpr bool operator==(const ModInt &x) const { return val == x.val; }
    constexpr bool operator!=(const ModInt &x) const { return val != x.val; }
    bool operator<(const ModInt &x) const { return val < x.val; }  // To use std::map<ModInt, T>
    friend std::istream &operator>>(std::istream &is, ModInt &x) { lint t; is >> t; x = ModInt(t); return is; }
    friend std::ostream &operator<<(std::ostream &os, const ModInt &x) { os << x.val;  return os; }
    constexpr lint power(lint n) const {
        lint ans = 1, tmp = this->val;
        while (n) {
            if (n & 1) ans = ans * tmp % mod;
            tmp = tmp * tmp % mod;
            n /= 2;
        }
        return ans;
    }
    constexpr lint inv() const { return this->power(mod - 2); }
};
using mint = ModInt<1000000007>;

int main()
{
    int N;
    lint L, R;
    cin >> N >> L >> R;
    vector<lint> fibs;
    {
        lint f1 = 1, f2 = 0;
        while (true) {
            lint fnew = f1 + f2;
            if (fnew >= L and fnew <= R) {
                fibs.emplace_back(fnew);
            }
            else if (fnew > R) break;
            f2 = f1;
            f1 = fnew;
        }
    }
    Trie<int> aho;
    for (auto v : fibs) {
        aho.add_keyword(to_string(v), 1);
    }
    aho.build_aho_corasick();
    const int V = aho.fail.size();
    vector<vector<int>> to(V, vector<int>(10));
    const vector<int> &bad = aho.node_info;
    REP(i, V) {
        REP(d, 10) {
            int now = i;
            while (now != 0 and aho.child[now].count('0' + d) == 0) {
                now = aho.fail[now];
            }
            if (aho.child[now].count('0' + d)) {
                to[i][d] = aho.child[now]['0' + d];
            }
        }
    }
    vector<mint> dp(V);
    dp[0] = 1;
    REP(_, N) {
        vector<mint> nxt(V);
        REP(i, V) for (auto j : to[i]) {
            if (!bad[j]) nxt[j] += dp[i];
        }
        dp = nxt;
    }
    cout << accumulate(dp.begin(), dp.end(), mint(0)) - 1 << '\n';
}
0