結果
| 問題 |
No.1269 I hate Fibonacci Number
|
| コンテスト | |
| ユーザー |
leaf_1415
|
| 提出日時 | 2020-10-23 23:15:47 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 464 ms / 3,000 ms |
| コード長 | 2,649 bytes |
| コンパイル時間 | 1,666 ms |
| コンパイル使用メモリ | 96,156 KB |
| 実行使用メモリ | 51,072 KB |
| 最終ジャッジ日時 | 2024-07-21 13:08:07 |
| 合計ジャッジ時間 | 4,361 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
ソースコード
#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define inf 1e18
#define mod 1000000007
using namespace std;
typedef long long llint;
typedef pair<llint, llint> P;
struct AhoCorasick{
struct TrieNode{
int num, sum;
int next[26], suff;
TrieNode(){
num = sum = 0;
for(int i = 0; i < 26; i++) next[i] = -1;
}
};
vector<TrieNode> trie;
AhoCorasick(){
init();
}
void init(){
trie.clear();
trie.push_back(TrieNode());
}
int seek(string &s)
{
int p = 0;
for(int i = 0; i < s.size(); i++){
int c = s[i]-'0';
if(trie[p].next[c] == -1){
trie.push_back(TrieNode());
trie[p].next[c] = (int)trie.size()-1;
}
p = trie[p].next[c];
}
return p;
}
int trans(int v, int c)
{
if(trie[v].next[c] != -1) return trie[v].next[c];
if(v == 0) return 0;
return trans(trie[v].suff, c);
}
void addString(string &s, int x = 1)
{
int p = seek(s);
trie[p].num += x;
}
void makeSuffixLink()
{
queue<int> Q;
Q.push(0);
trie[0].suff = 0;
int v;
while(Q.size()){
v = Q.front();
Q.pop();
trie[v].sum = trie[v].num + trie[trie[v].suff].sum;
for(int i = 0; i < 26; i++){
int u = trie[v].next[i];
if(u == -1) continue;
trie[u].suff = trans(trie[v].suff, i);
if(v == 0) trie[u].suff = 0;
Q.push(u);
}
}
}
};
llint n;
llint l, r;
vector<llint> vec;
vector<string> svec;
map<string, llint> mp;
AhoCorasick aho;
llint dp[5005][5005];
string get(llint x)
{
string ret;
for(;x;x/=10) ret += x%10 + '0';
reverse(all(ret));
return ret;
}
int main(void)
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> l >> r;
llint a = 0, b = 1;
while(1){
llint c = a+b;
if(c > 1e18) break;
if(c >= l && c <= r) vec.push_back(c);
a = b, b = c;
}
for(int i = 0; i < vec.size(); i++){
string s = get(vec[i]);
aho.addString(s);
}
aho.makeSuffixLink();
llint m = aho.trie.size();
dp[0][0] = 1;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
for(int k = 0; k < 10; k++){
int nj = aho.trans(j, k);
if(aho.trie[nj].sum == 0) (dp[i+1][nj] += dp[i][j]) %= mod;
}
}
}
llint ans = mod-1;
for(int i = 0; i < m; i++) ans += dp[n][i], ans %= mod;
cout << ans << endl;
return 0;
}
leaf_1415