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) + F(n - 2) (n >= 2)


  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
齐达拉图

题目大意:给出长度不超过40的串,表示一个斐波那契数列的前缀,如果在100000个斐波那契数里边找到了有这样前缀的一个斐波那契数,输出其编号。


分析:因为是维护前缀,所以在大数加法的时候,如果长度过长,我们需要去掉个位上的数字。对于每一个斐波那契数前缀都加入字典树中,然后输入进来的每一个查询即可。


AC代码:(感谢

Çαə▬杉
学长的代码0.0)

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    char c[50];
    int len;
}f[100010];
struct node1
{
	int nx[10];//0-9
	int now;//now表示编号
}tree[4500000];
void add(node &ret,node &a,node &b)
{
	int i,j,k,t,len=a.len;
	k=t=0;
	for(i=0;i<len;i++)
	{
		t=(a.c[i]+b.c[i]+k)/10;
		ret.c[i]=(a.c[i]+b.c[i]+k)%10;
		k=t;
	}
	ret.len=len;
	if(k!=0)
		ret.c[ret.len++]=k;
	if(ret.len>48)
	{
		for(i=0;i<ret.len;i++)
		{
			ret.c[i]=ret.c[i+1];
			a.c[i]=a.c[i+1];
		}
		ret.len--;
		a.len--;
	}
}
int num=1;//节点个数.
void insert(int k)
{
    int p=0;
    for(int i=f[k].len-1;i>=0;i--)
    {
        int tmp=f[k].c[i];
        if(tree[p].nx[tmp]==0)
        {
            tree[p].nx[tmp]=num++;
        }
        p=tree[p].nx[tmp];
        tree[p].now=k;
    }
}
int main()
{
    f[0].c[0]=1;f[0].len=1;
    f[1].c[0]=1;f[1].len=1;
    for(int i=2;i<=100005;i++)
    {
        add(f[i],f[i-1],f[i-2]);
    }
    for(int i=99999;i>=0;i--)
    {
        insert(i);
    }
    int t;
    int kase=0;
    scanf("%d",&t);
    while(t--)
    {
        char ss[50];
        int p=0,ans;
        scanf("%s",ss);
        for(int i=0;ss[i];i++)
        {
            int tmp=ss[i]-'0';
            if(tree[p].nx[tmp]==0)
            {
                ans=-1;
                break;
            }
            p=tree[p].nx[tmp];
            ans=tree[p].now;
        }
        printf("Case #%d: %d\n",++kase,ans);
    }
}








你可能感兴趣的:(HDU,哈理工oj,4099,1209)