加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 02:26:34 所属栏目:大数据 来源:网络整理
导读: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 follow

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
2011 Asia Shanghai Regional Contest
?

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]='';  
    //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("%sn",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("%dn",seach(temp));
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读