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

HDU 4099 Revenge of Fibonacci

时间:2014-09-14 23:45:17      阅读:501      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   io   os   java   ar   strong   

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
解题思路:大数+字典树,处理出前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;
}


HDU 4099 Revenge of Fibonacci

标签:des   style   http   color   io   os   java   ar   strong   

原文地址:http://blog.csdn.net/hqu_fritz/article/details/39274627

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