結果

問題 No.1269 I hate Fibonacci Number
ユーザー tsutajtsutaj
提出日時 2020-10-23 23:39:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,356 bytes
コンパイル時間 1,572 ms
コンパイル使用メモリ 121,612 KB
実行使用メモリ 62,412 KB
最終ジャッジ日時 2023-09-28 18:56:56
合計ジャッジ時間 5,001 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
62,352 KB
testcase_01 AC 19 ms
62,164 KB
testcase_02 AC 24 ms
62,124 KB
testcase_03 AC 19 ms
62,092 KB
testcase_04 AC 18 ms
62,108 KB
testcase_05 AC 18 ms
62,068 KB
testcase_06 AC 19 ms
62,368 KB
testcase_07 AC 18 ms
62,032 KB
testcase_08 AC 19 ms
62,020 KB
testcase_09 AC 19 ms
62,024 KB
testcase_10 AC 19 ms
62,164 KB
testcase_11 AC 19 ms
62,172 KB
testcase_12 AC 19 ms
62,172 KB
testcase_13 AC 64 ms
62,300 KB
testcase_14 AC 161 ms
62,124 KB
testcase_15 AC 39 ms
62,244 KB
testcase_16 AC 135 ms
62,140 KB
testcase_17 AC 21 ms
62,108 KB
testcase_18 AC 142 ms
62,136 KB
testcase_19 AC 113 ms
62,232 KB
testcase_20 AC 41 ms
62,104 KB
testcase_21 AC 89 ms
62,104 KB
testcase_22 AC 89 ms
62,236 KB
testcase_23 AC 91 ms
62,156 KB
testcase_24 AC 50 ms
62,248 KB
testcase_25 AC 65 ms
62,112 KB
testcase_26 AC 23 ms
62,080 KB
testcase_27 WA -
testcase_28 AC 28 ms
62,408 KB
testcase_29 AC 68 ms
62,164 KB
testcase_30 AC 40 ms
62,016 KB
testcase_31 AC 159 ms
62,160 KB
testcase_32 AC 60 ms
62,124 KB
testcase_33 AC 169 ms
62,180 KB
testcase_34 WA -
testcase_35 AC 40 ms
62,108 KB
testcase_36 AC 26 ms
62,296 KB
testcase_37 AC 19 ms
62,164 KB
testcase_38 AC 26 ms
62,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

void test() {
    vector<ll> fib(2, 1);
    for(int i=2; ; i++) {
        ll nxt_fib = fib[i-1] + fib[i-2];
        if(nxt_fib > (ll)1e18) break;
        else {
            fprintf(stderr, "fib[%d] = %lld\n", i, nxt_fib);
            fib.emplace_back(nxt_fib);
        }
    }
}

// ModInt begin

using ll = long long;
template<ll mod>
struct ModInt {
    ll v;
    ll mod_pow(ll x, ll n) const {
        return (!n) ? 1 : (mod_pow((x*x)%mod,n/2) * ((n&1)?x:1)) % mod;
    }
    ModInt(ll a = 0) : v((a %= mod) < 0 ? a + mod : a) {}
    ModInt operator+ ( const ModInt& b ) const {
        return (v + b.v >= mod ? ModInt(v + b.v - mod) : ModInt(v + b.v));
    }
    ModInt operator- () const {
        return ModInt(-v);
    }
    ModInt operator- ( const ModInt& b ) const {
        return (v - b.v < 0 ? ModInt(v - b.v + mod) : ModInt(v - b.v));
    }
    ModInt operator* ( const ModInt& b ) const {return (v * b.v) % mod;}
    ModInt operator/ ( const ModInt& b ) const {return (v * mod_pow(b.v, mod-2)) % mod;}
    
    bool operator== ( const ModInt &b ) const {return v == b.v;}
    bool operator!= ( const ModInt &b ) const {return !(*this == b); }
    ModInt& operator+= ( const ModInt &b ) {
        v += b.v;
        if(v >= mod) v -= mod;
        return *this;
    }
    ModInt& operator-= ( const ModInt &b ) {
        v -= b.v;
        if(v < 0) v += mod;
        return *this;
    }
    ModInt& operator*= ( const ModInt &b ) {
        (v *= b.v) %= mod;
        return *this;
    }
    ModInt& operator/= ( const ModInt &b ) {
        (v *= mod_pow(b.v, mod-2)) %= mod;
        return *this;
    }
    ModInt pow(ll x) { return ModInt(mod_pow(v, x)); }
    // operator int() const { return int(v); }
    // operator long long int() const { return v; }
};

template<ll mod>
ModInt<mod> pow(ModInt<mod> n, ll k) {
    return ModInt<mod>(n.mod_pow(n.v, k));
}

template<ll mod>
ostream& operator<< (ostream& out, ModInt<mod> a) {return out << a.v;}
template<ll mod>
istream& operator>> (istream& in, ModInt<mod>& a) {
    in >> a.v;
    return in;
}

// ModInt end

using mint = ModInt<MOD>;
int nxt_state[750][10];
mint dp[5010][750][2];
int main() {
    // test();
    ll N, L, R; cin >> N >> L >> R;
    
    vector<string> pfx;
    auto add_str = [&](string pat) {
                       if(stoll(pat) < L) return;
                       if(R < stoll(pat)) return;
                       for(int i=0; i<=(int)pat.size(); i++) {
                           pfx.emplace_back(pat.substr(0, i));
                       }
                   };
    
    add_str("1");
    vector<ll> fib(2, 1);
    for(int i=2; ; i++) {
        ll nxt_fib = fib[i-1] + fib[i-2];
        if(nxt_fib > (ll)1e18) break;
        else {
            add_str(to_string(nxt_fib));
            fib.emplace_back(nxt_fib);
        }
    }
    sort(pfx.begin(), pfx.end());
    pfx.erase(unique(pfx.begin(), pfx.end()), pfx.end());

    int M = pfx.size();
    vector<int> ng_state(M, false);
    for(int i=0; i<M; i++) {
        for(int k=1; k<(int)fib.size(); k++) {
            if(fib[k] < L or R < fib[k]) continue;
            string t = to_string(fib[k]);
            int lenA = t.length();
            int lenB = pfx[i].length();
            if(lenA <= lenB) {
                if(pfx[i].substr(lenB - lenA, lenA) == t) {
                    ng_state[i] = true;
                }
            }
        }

        for(int c=0; c<10; c++) {
            string s = pfx[i] + (char)('0' + c); int k;
            while(true) {
                k = lower_bound(pfx.begin(), pfx.end(), s) - pfx.begin();
                if(k < M and pfx[k] == s) break;
                s = s.substr(1);
            }
            nxt_state[i][c] = k;
        }
    }

    fill(dp[0][0], dp[N+1][0], mint(0));
    dp[0][0][0] = mint(1);
    for(int i=0; i<N; i++) {
        for(int j=0; j<M; j++) {
            for(int f=0; f<2; f++) {
                for(int k=0; k<10; k++) {
                    int nj = nxt_state[j][k];
                    if(ng_state[j] or ng_state[nj]) continue;
                    if(f == 0 and k == 0) continue;
                    int nf = f || (k > 0);
                    dp[i+1][nj][nf] += dp[i][j][f];
                }
            }
        }
    }
    
    mint ans(0);
    for(int i=0; i<M; i++) {
        for(int j=1; j<=N; j++) {
            if(ng_state[i]) continue;
            ans += dp[j][i][1];
        }
    }
    cout << ans << endl;
    return 0;
}
0