HDOJ 题目1789 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

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

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

时间: 2024-10-17 05:30:08

HDOJ 题目1789 Revenge of Fibonacci(大数, 字典树)的相关文章

hrbust 1209/hdu 4099 Revenge of Fibonacci【字典树+大数】

Revenge of Fibonacci Time Limit: 5000 MS Memory Limit: 204800 K Total Submit: 37(24 users) Total Accepted: 18(17 users) Rating:  Special Judge: No Description The well-known Fibonacci sequence is defined as following: F(0) = F(1) = 1 F(n) = F(n - 1)

HDOJ/HDU 1250 Hat&#39;s Fibonacci(大数~斐波拉契)

Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take

hdoj 1004 Let the Balloon Rise(模拟 || 字典树)

Let the Balloon Rise http://acm.hdu.edu.cn/showproblem.php?pid=1004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 84401    Accepted Submission(s): 31831 Problem Description Contest time again

HDOJ 题目5087 Revenge of LIS II(第二长LIS)

Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1195    Accepted Submission(s): 400 Problem Description In computer science, the longest increasing subsequence problem is to f

TOJ 3820 Revenge of Fibonacci(大数+trie)

描述 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 ha

HDOJ 题目1754 I Hate It(线段树单点更新,求区间最大值)

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 44713    Accepted Submission(s): 17548 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师

HDOJ 题目1166 敌兵布阵(线段树单点更新)

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 51751    Accepted Submission(s): 21648 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务

HDOJ 题目1698 Just a Hook(线段树区间更新)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 19862    Accepted Submission(s): 9964 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing

HDU 4099 Revenge of Fibonacci

Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 204800/204800 K (Java/Others) Total Submission(s): 2027    Accepted Submission(s): 475 Problem Description The well-known Fibonacci sequence is defined as following: Here w