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

解题思路:大数+字典树,处理出前10w的fibonocci数的前40位加入到字典树中,大数处理的时候用50位,避免进位错误,注意会爆内存

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <bitset>
#include <cmath>
#include <utility>
#define Maxn 100005
#define Maxm 1000005
#define lowbit(x) x&(-x)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PI acos(-1.0)
#define make_pair MP
#define LL long long
#define Inf (1LL<<62)
#define inf 0x3f3f3f3f
#define re freopen("in.txt","r",stdin)
#define wr freopen("out.txt","w",stdout)
using namespace std;
struct BigNum
{
    int num[55];
    int size;
    int tsize;
    BigNum()
    {
        memset(num,0,sizeof(num));
        size=1;
        tsize=1;
    }
    friend BigNum operator +(BigNum a,BigNum b)
    {
        BigNum c;
        int i;
        for(i=0;i<max(a.size,b.size);i++)
        {
            if(a.tsize>b.tsize&&a.tsize>50)
            {
                c.num[i]+=a.num[i]+b.num[i+1];
                c.num[i+1]+=c.num[i]/10;
                c.num[i]%=10;
            }
            else if(a.tsize<b.tsize&&b.tsize>50)
            {
                c.num[i]+=a.num[i+1]+b.num[i];
                c.num[i+1]+=c.num[i]/10;
                c.num[i]%=10;
            }
            else
            {
                c.num[i]+=a.num[i]+b.num[i];
                c.num[i+1]+=c.num[i]/10;
                c.num[i]%=10;
            }
        }
        c.size=c.num[i]?i+1:i;
        int tmp=c.size-1;
        if(max(a.tsize,b.tsize)<=50)
            c.tsize=max(a.size,b.size);
        else
            c.tsize=max(a.tsize,b.tsize);
        if(c.size>50)
        {
            int len=50;
            BigNum t;
            while(len--)
                t.num[len]=c.num[tmp--];
            t.tsize=max(a.tsize,b.tsize)+1;
            c=t;
            c.size=50;
        }
        return c;
    }
    void output(BigNum n)
    {
        int i;
        for(i=n.size-1;i>=0;i--)
            printf("%d",n.num[i]);
        cout<<" "<<n.tsize;
        printf("\n");
    }
};
BigNum fib[3];
struct TrieNode
{
    int index;
    TrieNode *next[10];
    TrieNode()
    {
        index=inf;
        memset(next,0,sizeof(next));
    }
};
TrieNode *root=NULL;
void CreatTree(int *s,int len,int index)
{
    int i,cnt;
    TrieNode *p=root;
    TrieNode *temp;
    for(i=len-1,cnt=0;i>=0&&cnt<=40;i--,cnt++)
    {
        if(p->next[s[i]]==NULL)
        {
            temp=new TrieNode;
            p->next[s[i]]=temp;
        }
        p=p->next[s[i]];
        p->index=min(p->index,index);
    }
}
int search(int *s,int len)
{
	int i;
	TrieNode *p=root;
	TrieNode *tmp;
	for(i=0;i<len;i++)
	{
		if(p->next[s[i]]==NULL)
			return -1;
		p=p->next[s[i]];
	}
	return p->index;
}
void Delete(TrieNode *node)
{
    int i;
    for(i=0;i<10;i++)
    {
        if(node->next[i])
            Delete(node->next[i]);
        delete node->next[i];
        node->next[i]=0;
    }
}
int main()
{
	int n,ncase=1,arr[50];
	char str[50];
	fib[0].num[0]=1;fib[1].num[0]=1;
	root=new TrieNode;
	CreatTree(fib[0].num,fib[0].size,0);
	CreatTree(fib[1].num,fib[1].size,1);
	//re;wr;
	for(int i=2;i<100000;i++)
	{
		fib[i%3]=fib[(i-1)%3]+fib[(i-2)%3];
        //fib[i%3].output(fib[i%3]);
		CreatTree(fib[i%3].num,fib[i%3].size,i);
	}
	scanf("%d",&n);
	while(n--)
	{
		printf("Case #%d: ",ncase++);
		memset(arr,0,sizeof(arr));
		scanf("%s",str);
		int len=strlen(str);
		for(int i=0;i<len;i++)
			arr[i]=str[i]-'0';
		printf("%d\n",search(arr,len));
		memset(str,'\0',sizeof(str));
	}
    Delete(root);
	return 0;
}
时间: 2025-01-18 11:54:11

HDU 4099 Revenge of Fibonacci的相关文章

HDU 4099 Revenge of Fibonacci Trie+高精度

Revenge of Fibonacci 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

hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法

Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输出-1: 思路:直接使用数组模拟加法,再用Trie树插入查找即可:但是一般使用new Trie()的代码都是MLE的.反而我之前写的,直接得到数组大小的maxnode版本的内存可以接受:并且还有一点就是前40位的精度问题:由于是自己计算出来的finboncci数列,并不是系统给的,所以1的进位不会形成递推

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): 2355    Accepted Submission(s): 587 Problem Description The well-known Fibonacci sequence is defined as following: Here w

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)

hdu 5018 Revenge of Fibonacci(BestCoder Round #10)

Revenge of Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 372    Accepted Submission(s): 177 Problem Description In mathematical terms, the sequence Fn of Fibonacci numbers is define

HDU 5018 Revenge of Fibonacci(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5018 Problem Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2 with seed values F1 = 1; F2 = 1 (sequence A000045 in OEIS). ---

hdu 5018 Revenge of Fibonacci

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5018 思路:直接计算判断就是啦,注意一点要判断给出的A,B.... code #include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #include<cstring> using namespace std; int main() { int F[1000];

Revenge of Fibonacci(杭电5018)

Revenge of Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 721    Accepted Submission(s): 332 Problem Description In mathematical terms, the sequence Fn of Fibonacci numbers is define

HDU 4099 大数+Trie

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