码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-31 22:03:35      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

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,0,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));
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:

原文地址:http://blog.csdn.net/yu_ch_sh/article/details/47175077

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!