結果

問題 No.1269 I hate Fibonacci Number
ユーザー leaf_1415leaf_1415
提出日時 2020-10-23 23:15:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 419 ms / 3,000 ms
コード長 2,649 bytes
コンパイル時間 2,208 ms
コンパイル使用メモリ 88,352 KB
実行使用メモリ 51,244 KB
最終ジャッジ日時 2023-09-28 18:27:31
合計ジャッジ時間 5,451 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 4 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 86 ms
26,468 KB
testcase_14 AC 106 ms
32,996 KB
testcase_15 AC 20 ms
13,624 KB
testcase_16 AC 78 ms
21,128 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 86 ms
24,464 KB
testcase_19 AC 67 ms
27,304 KB
testcase_20 AC 19 ms
10,644 KB
testcase_21 AC 52 ms
17,168 KB
testcase_22 AC 47 ms
18,828 KB
testcase_23 AC 55 ms
23,232 KB
testcase_24 AC 24 ms
9,200 KB
testcase_25 AC 35 ms
16,900 KB
testcase_26 AC 5 ms
5,312 KB
testcase_27 AC 10 ms
10,824 KB
testcase_28 AC 15 ms
6,660 KB
testcase_29 AC 44 ms
26,008 KB
testcase_30 AC 24 ms
18,632 KB
testcase_31 AC 172 ms
28,940 KB
testcase_32 AC 31 ms
11,772 KB
testcase_33 AC 419 ms
51,244 KB
testcase_34 AC 24 ms
23,368 KB
testcase_35 AC 28 ms
24,180 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0