結果

問題 No.1269 I hate Fibonacci Number
ユーザー hitonanode
提出日時 2020-10-24 19:13:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 61 ms / 3,000 ms
コード長 5,275 bytes
コンパイル時間 2,905 ms
コンパイル使用メモリ 222,032 KB
最終ジャッジ日時 2025-01-15 15:20:55
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

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