您的位置:首页 > 其它

HDU4099 Revenge of Fibonacci(高精度+Trie)

2015-11-22 17:17 316 查看

              Revenge of Fibonacci

                          Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/Others)
                                 Total Submission(s): 2582 Accepted Submission(s): 647


[align=left]Problem Description[/align]
The well-known Fibonacci sequence is defined as following:

Here we regard n as the index of the Fibonacci number F(n).
This
sequence has been studied since the publication of Fibonacci's book
Liber Abaci. So far, many properties of this sequence have been
introduced.
You had been interested in this sequence, while after
reading lots of papers about it. You think there’s no need to research
in it anymore because of the lack of its unrevealed properties.
Yesterday, you decided to study some other sequences like Lucas sequence
instead.
Fibonacci came into your dream last night. “Stupid human
beings. Lots of important properties of Fibonacci sequence have not been
studied by anyone, for example, from the Fibonacci number 347746739…”
You
woke up and couldn’t remember the whole number except the first few
digits Fibonacci told you. You decided to write a program to find this
number out in order to continue your research on Fibonacci sequence.

[align=left]Input[/align]
There
are multiple test cases. The first line of input contains a single
integer T denoting the number of test cases (T<=50000).
For each
test case, there is a single line containing one non-empty string made
up of at most 40 digits. And there won’t be any unnecessary leading
zeroes.

[align=left]Output[/align]
For
each test case, output the smallest index of the smallest Fibonacci
number whose decimal notation begins with the given digits. If no
Fibonacci number with index smaller than 100000 satisfy that condition,
output -1 instead – you think what Fibonacci wants to told you beyonds
your ability.

[align=left]Sample Input[/align]

15
1
12
123
1234
12345
9
98
987
9876
98765
89
32
51075176167176176176
347746739
5610

[align=left]Sample Output[/align]

Case #1: 0
Case #2: 25
Case #3: 226
Case #4: 1628
Case #5: 49516
Case #6: 15
Case #7: 15
Case #8: 15
Case #9: 43764
Case #10: 49750
Case #11: 10
Case #12: 51
Case #13: -1
Case #14: 1233
Case #15: 22374

[align=left]Source[/align]
2011 Asia Shanghai Regional Contest
【思路】

高精度加法+Trie。

离线处理出F(99999)以内所有的F在长度40以内的前缀并构造一棵字典树。因为精度原因,保留60位进行加法计算就可以达到精确。

Trie的结点维护一个val表示经过该节点的所有字符串中的最小标号,对应每一个查询用O(maxn)的时间求解。

   总的时间复杂度为O(99999*60+T*maxn)。

【代码】

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std;

const int sigmasize = 10;
//Trie相关
struct Node{
int val;
Node* next[sigmasize];
};
struct Trie{
Node *root;
Trie() {
root=new Node;
for(int i=0;i<sigmasize;i++) root->next[i]=NULL;
root->val=0;
}
int idx(char c) { return c-'0'; }
void insert(char* s,int v) {
int n=strlen(s);
Node* u=root;
for(int i=0;i<min(n,41);i++) {
int c=idx(s[i]);
if(u->next[c]==NULL) {
Node* tmp=new Node;
tmp->val=0;
for(int i=0;i<sigmasize;i++) tmp->next[i]=NULL;
u->next[c]=tmp;
}
u=u->next[c];
if(u->val==0) u->val=v;
}
if(u->val==0) u->val=v;   //存储最小的F
}
int find(char* s) {
int n=strlen(s);
Node* u=root;
for(int i=0;i<min(n,41);i++) {
int c=idx(s[i]);
if(u->next[c]==NULL) return 0;
else u=u->next[c];
}
return u->val;
}
void del(Node *root) {
for(int i=0;i<sigmasize;i++) {
if(root->next[i]!=NULL) del(root->next[i]);
}
free(root);
}
}trie;
//题目相关
int n;
const int maxn = 100;
char F1[maxn],F2[maxn],Ftmp[maxn],s[maxn];
char d[maxn];

void Add(char *a,char *b,char *c)
{
int i,j,k,aa,bb,t=0,p=0;
aa=strlen(a)-1,bb=strlen(b)-1;
while(aa>=0||bb>=0) {
if(aa<0)i=0;   else i=a[aa]-'0';
if(bb<0)j=0;   else j=b[bb]-'0';
k=i+j+t;
d[p++]=k%10+'0';
t=k/10;
aa--,bb--;
}
while(t) {
d[p++]=t%10+'0';
t=t/10;
}
for(i=0;i<p;i++) c[i]=d[p-i-1];
c[p]='\0';
}

int main() {
F1[0]='1',F1[1]='\0';
F2[0]='1',F2[1]='\0';
trie.insert(F1,1),trie.insert(F2,1);
FOR(i,2,100000-1) {
strcpy(Ftmp,F2);
int len1=strlen(F1),len2=strlen(F2);
if(len1>60) {
F1[60]=F2[60]='\0';
}
Add(F1,F2,F2);
trie.insert(F2,i+1);
strcpy(F1,Ftmp);
}
int T,kase=0;
scanf("%d",&T);
while(T--) {
scanf("%s",s);
int ans=trie.find(s);
if(!ans) ans=-1;  else ans--;
printf("Case #%d: %d\n",++kase,ans);
}
trie.del(trie.root);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: