結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 4 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 51 ms
4,376 KB
testcase_15 AC 7 ms
4,376 KB
testcase_16 AC 63 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 64 ms
4,376 KB
testcase_19 AC 34 ms
4,376 KB
testcase_20 AC 10 ms
4,376 KB
testcase_21 AC 34 ms
4,380 KB
testcase_22 AC 27 ms
4,380 KB
testcase_23 AC 32 ms
4,376 KB
testcase_24 AC 18 ms
4,380 KB
testcase_25 AC 19 ms
4,380 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 RE -
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 16 ms
4,380 KB
testcase_30 AC 7 ms
4,380 KB
testcase_31 AC 19 ms
4,376 KB
testcase_32 AC 22 ms
4,376 KB
testcase_33 AC 3 ms
4,376 KB
testcase_34 RE -
testcase_35 AC 5 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 3 ms
4,376 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 nextNode = nodes[i].next[j];
				if(nextNode!=-1){
					ndp[nextNode] += dp[i];
				}
				else{
					int cNode = i;
					while(nodes[cNode].link != -1&&cNode!=0){
						cNode = nodes[cNode].link;
						nextNode = nodes[cNode].next[j];
						if(nextNode!=-1){
							break;
						}
					}
					if(nextNode!=-1){
						ndp[nextNode] += dp[i];
					}
					else{
						ndp[0] += 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[0] < L)F.erase(F.begin());
	
	vector<string> S = get(F);
	
	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);
	}
	
	mint ans = 0;
	rep(i,dp.size())ans += dp[i];
	
	ans --;
	
	cout<<ans.val()<<endl;
	
    return 0;
}
0