結果

問題 No.1269 I hate Fibonacci Number
ユーザー 沙耶花沙耶花
提出日時 2020-10-23 23:53:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 43 ms / 3,000 ms
コード長 3,747 bytes
コンパイル時間 2,828 ms
コンパイル使用メモリ 225,216 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-28 19:13:47
合計ジャッジ時間 4,869 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 4 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 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,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 38 ms
4,376 KB
testcase_15 AC 6 ms
4,376 KB
testcase_16 AC 41 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 43 ms
4,380 KB
testcase_19 AC 26 ms
4,380 KB
testcase_20 AC 8 ms
4,376 KB
testcase_21 AC 25 ms
4,380 KB
testcase_22 AC 20 ms
4,376 KB
testcase_23 AC 24 ms
4,376 KB
testcase_24 AC 12 ms
4,376 KB
testcase_25 AC 14 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 13 ms
4,380 KB
testcase_30 AC 5 ms
4,376 KB
testcase_31 AC 16 ms
4,380 KB
testcase_32 AC 16 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 5 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/modint>
using namespace atcoder;
using mint = modint1000000007;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000000000000
long long L,R;
vector<string> get(vector<long long> F){
	vector<string> S;
	rep(i,F.size()){
		S.push_back(to_string(F[i]));
	}
	while(true){
		bool f = false;
		rep(i,S.size()){
			rep(j,S.size()){
				if(j<=i)continue;
				rep(k,S[j].size()){
					if(k+S[i].size()>S[j].size())break;
					if(S[j].substr(k,S[i].size())==S[i]){
						S.erase(S.begin()+j);
						f=true;
						break;
					}
				}
				if(f)break;
			}
			if(f)break;
		}
		if(!f)break;
	}
	return S;
}
vector<mint> dp,ndp;
template <typename T>
struct trie{
	T init_value;
	struct node{
		vector<int> next;
		T v;
		T sum;
		int depth;
		node(int wordSize,T iv,int d){
			next.resize(wordSize,-1);
			v = iv;
			sum = iv;
			depth = d;
		}
		
		int link=-1;
	};
	vector<node> nodes;

	int wordSize;
	trie(int sz,T iv){
		init_value = iv;
		wordSize = sz;
		nodes.push_back(node(wordSize,init_value,0));
	}
	
	void add(string &S,T x,int cPos=0,int cNode=0){
		if(cPos==S.size()){
			nodes[cNode].v = func(nodes[cNode].v,x);
			return;
		}
		int c = encode(S[cPos]);
		if(nodes[cNode].next[c]==-1){
			nodes[cNode].next[c] = nodes.size();
			nodes.push_back(node(wordSize,init_value,nodes[cNode].depth+1));
		}
		
		int nextNode = nodes[cNode].next[c];
		add(S,x,cPos+1,nextNode);
	}
	
	void set_link(){
		nodes[0].link = 0;
		queue<int> Q;
		Q.push(0);
		while(Q.size()!=0){
			int now = Q.front();
			Q.pop();
			nodes[now].v = func(nodes[now].v,nodes[nodes[now].link].v);
			nodes[now].sum = func(nodes[now].sum,nodes[now].v);
			for(int i=0;i<wordSize;i++){
				int to = nodes[now].next[i];
				if(to==-1)continue;
				int x = now;
				while(x!=0){
					x = nodes[x].link;
					if(nodes[x].next[i]!=-1){
						x = nodes[x].next[i];
						break;
					}
				}
				nodes[to].link = x;
				nodes[to].sum = func(nodes[to].sum,nodes[now].sum);
				Q.push(to);
			}
		}
	}
	
	T query(string &S,int cPos = 0,int cNode=0){
		T ret = init_value;
		if(cPos==S.size())return ret;
		int c = encode(S[cPos]);
		
		int nextNode = nodes[cNode].next[c];
		if(nextNode==-1){
			if(nodes[cNode].link!=-1){
				if(cNode!=0)ret = func(ret,query(S,cPos,nodes[cNode].link));
				else ret = func(ret,query(S,cPos+1,cNode));
			}
		}
		else{
			ret = func(ret,nodes[nextNode].v);
			ret = func(ret,query(S,cPos+1,nextNode));
		}
		
		return ret;
		
	}
	
	void go(){
		rep(i,nodes.size()){
			rep(j,10){
				int cNode = i;
				int nextNode = nodes[cNode].next[j];
				while(nextNode == -1){
					if(nodes[cNode].link==-1){
						nextNode = 0;
						break;
					}
					else{
						cNode = nodes[cNode].link;
						nextNode = nodes[cNode].next[j];
						if(nextNode==-1 && cNode==0){
							nextNode = 0;
							break;
						}
					}
				}
				ndp[nextNode] += dp[i];
			}
		}
		
		rep(i,nodes.size()){
			if(nodes[i].v<0){
				ndp[i] = 0;
			}
		}
	}
	
	int encode(char c){
		return c-'0';
	}
	
	T func(T a,T b){
		return a+b;
	}
	
};

int main(){	
	int N;
	cin>>N>>L>>R;
	vector<long long> F(2,1);
	
	while(true){
		if(F.back() > R){
			F.pop_back();
			break;
		}
		F.push_back(F.back() + F[F.size()-2]);
	}
	
	while(F.size()>0&&F[0] < L)F.erase(F.begin());
	
	vector<string> S = get(F);
	mint ans = 0;
	if(S.size()==0){
		ans = 1;
		rep(i,N)ans *= 10;
		ans--;
	}
	else{
		
	
		trie<int> T(10,0);
		
		rep(i,S.size()){
			T.add(S[i],-1);
		}
		T.set_link();
		dp.resize(T.nodes.size(),0);
		dp[0] = 1;

		rep(i,N){
			ndp.assign(T.nodes.size(),0);
			T.go();
			swap(dp,ndp);
		}
		
		
		rep(i,dp.size())ans += dp[i];
		
		ans --;
	}
	
	cout<<ans.val()<<endl;
	
    return 0;
}
0