結果

問題 No.1269 I hate Fibonacci Number
ユーザー lorent_kyoprolorent_kyopro
提出日時 2020-10-24 01:42:23
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 3,000 ms
コード長 1,972 bytes
コンパイル時間 2,800 ms
コンパイル使用メモリ 217,872 KB
実行使用メモリ 17,516 KB
最終ジャッジ日時 2023-09-28 19:58:50
合計ジャッジ時間 4,763 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 24 ms
7,432 KB
testcase_14 AC 53 ms
8,576 KB
testcase_15 AC 8 ms
4,376 KB
testcase_16 AC 48 ms
7,184 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 49 ms
7,524 KB
testcase_19 AC 36 ms
6,568 KB
testcase_20 AC 11 ms
4,376 KB
testcase_21 AC 30 ms
5,896 KB
testcase_22 AC 28 ms
5,588 KB
testcase_23 AC 29 ms
6,080 KB
testcase_24 AC 15 ms
4,572 KB
testcase_25 AC 18 ms
4,912 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 8 ms
4,376 KB
testcase_29 AC 18 ms
4,932 KB
testcase_30 AC 7 ms
4,380 KB
testcase_31 AC 65 ms
9,772 KB
testcase_32 AC 19 ms
4,904 KB
testcase_33 AC 98 ms
17,516 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 7 ms
4,376 KB
testcase_36 AC 7 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 7 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    int n;
    ll l, r;
    cin >> n >> l >> r;

    ll a = 1, b = 1;
    vector<string> s;
    while (b <= r) {
        if (l <= b) s.push_back(to_string(b));
        ll c = a + b;
        a = b; b = c;
    }

    vector<string> prefix;
    prefix.push_back("");
    for (auto& si : s) {
        for (size_t i = 0; i <= si.size(); ++i) {
            prefix.push_back(si.substr(0, i));
        }
    }
    sort(prefix.begin(), prefix.end());
    prefix.erase(unique(prefix.begin(), prefix.end()), prefix.end());

    int m = prefix.size();
    vector<bool> ng(m);
    vector nxt(m, vector(10, 0));

    for (int i = 0; i < m; ++i) {
        for (auto& si : s) {
            if (si.size() <= prefix[i].size() && si == prefix[i].substr(prefix[i].size() - si.size(), si.size())) {
                ng[i] = true;
                break;
            }
        }
        for (int j = 0; j < 10; ++j) {
            char c = '0' + j;
            string t = prefix[i] + c;
            int k;
            while (true) {
                k = distance(prefix.begin(), lower_bound(prefix.begin(), prefix.end(), t));
                if (k < m && prefix[k] == t) break;
                t = t.substr(1);
            }
            nxt[i][j] = k;
        }
    }

    const int mod = 1000000007;
    vector dp(n+1, vector(m, 0));
    dp[0][0] = 1;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (ng[j]) continue;
            for (int k = 0; k < 10; ++k) {
                int nj = nxt[j][k];
                if (ng[nj]) continue;
                if (i == 0 && k == 0) continue;
                dp[i+1][nj] += dp[i][j];
                dp[i+1][nj] %= mod;
            }
        }
    }

    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < m; ++j) {
            ans += dp[i][j];
            ans %= mod;
        }
    }
    cout << ans << '\n';
}
0