微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

HDOJ 题目4099 Revenge of Fibonacci大数, 字典树

Revenge of Fibonacci

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


Problem Description
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.
 

Input
  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.
 

Output
  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.
 

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

Sample Output
  
  
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
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   4091  4097  4096  4093  4092 
 
坑题啊、、
ac代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char a[100],b[100],c[100],t[100];
typedef struct s
{
	struct s * child[10];
	int id;
}node,*Node;
Node root;
void add(char a[],char b[],char c[])  
{  
    int len1,len2,i,j,t[10000],max,k=0;  
    len1=strlen(a);  
    len2=strlen(b);  
    i=len1-1;  
    j=len2-1;  
    memset(t,sizeof(t));  
    while(i>=0||j>=0)  
    {  
        if(i<0&&j>=0)  
            t[k]+=b[j]-'0';  
        else  
            if(j<0&&i>=0)  
                t[k]+=a[i]-'0';  
            else  
            {  
                t[k]+=a[i]-'0'+b[j]-'0';  
            }  
        k++;  
        t[k]+=t[k-1]/10;  
        t[k-1]%=10;  
        if(t[k])  
            max=k;  
        else  
            max=k-1;  
        i--;  
        j--;  
    }  
    for(i=max;i>=0;i--)  
        c[max-i]=t[i]+'0';  
    c[max+1]='\0';  
    //return c;  
}  
char str[3][100];
void insert(char *s,int id)
{
	Node cur,newnode;
	int i,Now,len;
	len=strlen(s);
	cur=root;
	//int i;
	for(i=0;i<len&&i<41;i++)
	{
		Now=s[i]-'0';
		if(cur->child[Now]!=NULL)
		{
			cur=cur->child[Now];
		}
		else
		{
			newnode=(Node)calloc(1,sizeof(node));
			cur->child[Now]=newnode;
			cur=cur->child[Now];
			cur->id=id;
		}
	}
}
int seach(char *s)
{
	int len=strlen(s),i;
	Node cur;
	cur=root;
	for(i=0;i<len;i++)
	{
		int Now=s[i]-'0';
		if(cur->child[Now]==NULL)
			break;
		cur=cur->child[Now];
	}
	if(i<len)
		return -1;
	return cur->id;
}
void fun()
{
	strcpy(a,"1");
	insert(a,0);
	strcpy(b,"1");
	insert(b,1);
	for(int i=2;i<100000;i++)
	{
		int len1=strlen(a);
		int len2=strlen(b);
		if(len2>60)
		{
			b[len2-1]=0;
			a[len1-1]=0;
		}
		add(a,b,t);
		insert(t,i);
		strcpy(a,b);
		strcpy(b,t);
	//	if(i<30)
	//		printf("%s\n",t);
	}
}
int main()
{
	int t,c=0;
	root=(Node)calloc(1,sizeof(node));
	fun();
	scanf("%d",&t);
	while(t--)
	{
		char temp[50];
		scanf("%s",temp);
		printf("Case #%d: ",++c);
		printf("%d\n",seach(temp));
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐