結果

問題 No.1269 I hate Fibonacci Number
ユーザー milanis48663220milanis48663220
提出日時 2022-01-10 17:01:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 366 ms / 3,000 ms
コード長 7,156 bytes
コンパイル時間 2,339 ms
コンパイル使用メモリ 153,348 KB
実行使用メモリ 31,232 KB
最終ジャッジ日時 2024-04-26 20:10:42
合計ジャッジ時間 5,242 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 7 ms
5,376 KB
testcase_03 AC 1 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 68 ms
11,264 KB
testcase_14 AC 186 ms
13,568 KB
testcase_15 AC 16 ms
5,376 KB
testcase_16 AC 156 ms
10,752 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 170 ms
11,392 KB
testcase_19 AC 97 ms
9,216 KB
testcase_20 AC 26 ms
5,376 KB
testcase_21 AC 102 ms
8,192 KB
testcase_22 AC 78 ms
7,552 KB
testcase_23 AC 87 ms
8,192 KB
testcase_24 AC 48 ms
5,376 KB
testcase_25 AC 50 ms
6,144 KB
testcase_26 AC 5 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 17 ms
5,376 KB
testcase_29 AC 45 ms
6,144 KB
testcase_30 AC 14 ms
5,376 KB
testcase_31 AC 255 ms
15,744 KB
testcase_32 AC 58 ms
6,144 KB
testcase_33 AC 366 ms
31,232 KB
testcase_34 AC 3 ms
5,376 KB
testcase_35 AC 11 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

const ll MOD = 1000000007;

class ModInt{
    public:
    ll v;
    ModInt(ll _v = 0){
        if(_v < 0) _v = (_v%MOD)+MOD;
        if(_v >= MOD) _v %= MOD;
        v = _v;
    }
    ModInt operator+(ll n){
        return ModInt((v+n)%MOD);
    }
    ModInt operator-(ll n){
        return ModInt((v-n+MOD)%MOD);
    }
    ModInt operator*(ll n){
        if(n >= MOD) n %= MOD;
        return ModInt((v*n)%MOD);
    }
    ModInt operator/(ll n){
        return ModInt((ModInt(n).inv()*v).v%MOD);
    }

    ModInt &operator+=(ll n){
        v = (v+n)%MOD;
        return *this;
    }
    ModInt &operator-=(ll n){
        v = (v-n+MOD)%MOD;
        return *this;
    }
    ModInt &operator*=(ll n){
        v = (v*n+MOD)%MOD;
        return *this;
    }
    
    
    ModInt operator+(ModInt n){
        return ModInt((v+n.v)%MOD);
    }
    ModInt operator-(ModInt n){
        return ModInt((v-n.v+MOD)%MOD);
    }
    ModInt operator*(ModInt n){
        return ModInt((v*n.v)%MOD);
    }
    ModInt operator/(ModInt n){
        return ModInt((n.inv()*v).v%MOD);
    }

    ModInt &operator+=(ModInt n){
        v = (v+n.v)%MOD;
        return *this;
    }
    ModInt &operator-=(ModInt n){
        v = (v-n.v+MOD)%MOD;
        return *this;
    }
    ModInt &operator*=(ModInt n){
        v = (v*n.v)%MOD;
        return *this;
    }

    bool operator==(ModInt n){
        return v == n.v;
    }
    bool operator!=(ModInt n){
        return v != n.v;
    }
    ModInt &operator=(ll n){
        v = n%MOD;
        return *this;
    }

    ModInt inv(){
        if(v == 1) return ModInt(1);
        else return ModInt(MOD-ModInt(MOD%v).inv().v*(MOD/v)%MOD);
    }
};

ostream& operator<<(ostream& os, const ModInt& m){
    os << m.v;
    return os;
}

istream & operator >> (istream &in,  ModInt &m){
    in >> m.v;
    return in;
}

ModInt pow(ModInt a, ll n) {
    assert(n >= 0);
	ModInt ans = 1;
    while (n > 0) {
        if (n&1) ans = ans*a;
        a = a*a;
        n >>= 1;
    }
	return ans;
}

using mint = ModInt;

class AhoCorasick {
    public:
    struct Edge{
        int to;
        char c;
        Edge(int to, char c): to(to), c(c) { }
    };
    vector<string> key_words;
    int n_nodes;
    AhoCorasick(vector<string> key_words): key_words(key_words) {
        n_nodes = 1;
        output = {{}};
        tree = {{}};
        for(int i = 0; i < key_words.size(); i++) {
            add(i);
        }
        // build failure and output
        failure.resize(n_nodes);
        failure[0] = 0;
        queue<int> que;
        for(Edge e: tree[0]){
            que.push(e.to);
            failure[e.to] = 0;
        }
        while(!que.empty()){
            int v = que.front(); que.pop();
            for(Edge e: tree[v]){
                que.push(e.to);
                int u = failure[v];
                while(find_node(u, e.c) == -1 && u != 0) {
                    u = failure[u];
                }
                failure[e.to] = find_node(u, e.c);
                if(u == 0 && failure[e.to] == -1){
                    failure[e.to] = 0;
                }
                for(int i: output[failure[e.to]]) output[e.to].insert(i);
            }
        }
    }
    vector<int> search_text(string text){
        int cur = 0;
        vector<int> ans(key_words.size());
        for(char c: text){
            while(true){
                int nx = find_node(cur, c);
                if(cur == 0 && nx == -1){
                    nx = 0;
                }
                if(nx != -1) {
                    cur = nx;
                    break;
                }
                cur = failure[cur];
            }
            for(int i: output[cur]) ans[i]++;
        }
        return ans;
    }       
    vector<vector<Edge>> tree;
    vector<int> failure;
    vector<set<int>> output;
    void add(int idx){
        int cur = 0;
        for(char c: key_words[idx]){
            int nx = find_node(cur, c);
            if(nx == -1){
                nx = add_node(cur, c);
            }
            cur = nx;
        }
        output[cur].insert(idx);
    }
    
    int find_node(int i, char c){
        for(Edge e: tree[i]){
            if(e.c == c) return e.to;
        }
        return -1;
    }
    int add_node(int from, char c){
        int to = n_nodes;
        tree[from].push_back(Edge(to, c));
        n_nodes++;
        output.push_back({});
        tree.push_back({});
        return to;
    }
};

vector<ll> list_fib(ll l, ll r){
    vector<ll> fib = {1, 2};
    for(int i = 2;;i++){
        ll x = fib[i-1]+fib[i-2];
        if(x > r) break;
        fib.push_back(x);
    }
    vector<ll> ans;
    for(ll x: fib){
        if(l <= x && x <= r) ans.push_back(x);
    }
    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; ll l, r; cin >> n >> l >> r;
    auto fib = list_fib(l, r);
    vector<string> key_words;
    for(ll x: fib) key_words.push_back(to_string(x));
    auto ac = AhoCorasick(key_words);
    auto dp = vec2d(n+1, ac.n_nodes, mint(0));
    dp[0][0] = 1;
    vector<bool> ok(ac.n_nodes, true);
    for(int i = 0; i < ac.n_nodes; i++){
        if(!ac.output[i].empty()) ok[i] = false;
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < ac.n_nodes; j++){
            if(!ok[j]) continue;
            for(int k = 0; k < 10; k++){
                int cur = j;
                char c = '0'+k;
                while(true){
                    int nx = ac.find_node(cur, c);
                    if(cur == 0 && nx == -1){
                        nx = 0;
                    }
                    if(nx != -1) {
                        cur = nx;
                        break;
                    }
                    cur = ac.failure[cur];
                }
                if(!ok[cur]) continue;
                dp[i+1][cur] += dp[i][j];
            }
        }
    }
    cout << accumulate(dp[n].begin(), dp[n].end(), mint(0))-1 << endl;
}
0