結果
問題 |
No.1269 I hate Fibonacci Number
|
ユーザー |
|
提出日時 | 2024-11-08 14:20:16 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 407 ms / 3,000 ms |
コード長 | 2,167 bytes |
コンパイル時間 | 4,163 ms |
コンパイル使用メモリ | 262,036 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-08 14:20:23 |
合計ジャッジ時間 | 6,656 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; constexpr int MOD = 1e9 + 7; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, l, r) for (int i = (l); i < (int)(r); i++) #define all(v) v.begin(), v.end() struct Aho_lc { int X; bool init; static constexpr int sigma = 10; vector<array<int, sigma>> to; vector<int> cnt, link; Aho_lc() : cnt(1), to(1), init(0) { rep(i, sigma) to[0][i] = -1; } void add(const string &s) { int v = 0; for (char i : s) { i -= '0'; if (to[v][i] == -1) { to[v][i] = (int)to.size(); to.push_back(array<int, sigma>()); cnt.push_back(0); rep(j, sigma) to.back()[j] = -1; } v = to[v][i]; } cnt[v]++; } void build() { assert(!init); init = 1; link.assign(to.size(), -1); queue<int> bfs; bfs.push(0); while (bfs.size()) { int v = bfs.front(); bfs.pop(); rep(i, sigma) { int u = to[v][i]; if (u == -1) continue; link[u] = _link_search(link[v], i); cnt[u] += cnt[link[u]]; bfs.push(u); } } } int _link_search(int v, int c) { while (v != -1) { if (to[v][c] != -1) return to[v][c]; v = link[v]; } return 0; } }; void ch(ll &a, ll b) { a = (a + b) % MOD; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N, L, R; cin >> N >> L >> R; vector<string> ng; ll r1 = 1, r2 = 1; while (r2 <= R) { if (r2 >= L) ng.push_back(to_string(r2)); r2 += r1; r1 = r2 - r1; } Aho_lc aho; for (auto i : ng) aho.add(i); aho.build(); int M = aho.cnt.size(); vector dp(M, 0ll); dp[0] = 1; rep(i, N) { vector ndp(M, 0ll); rep(j, M) { rep(k, 10) { int v = j, u = 0; while (v != -1) { if (aho.to[v][k] != -1) { u = aho.to[v][k]; break; } v = aho.link[v]; } if (!aho.cnt[u]) ch(ndp[u], dp[j]); } } rep(j, M) { dp[j] = ndp[j]; } } ll ans = 0; rep(i, M) { ch(ans, dp[i]); } ch(ans, MOD - 1); cout << ans << '\n'; }