首页 > 代码库 > HDU 4099 大数+Trie

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 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
debug三天竟是数据范围看错的惨案,将<=10w改成<10w后AC   = =
还有一点关于大数模拟,一开始string一直错我以为是这个问题就改成手打的模拟大数加法,
关于这个char数组并不会自动初始化   假如  char s[35];  for(int i=0;i<10;++i) s[i]=i+‘0‘; 就会出现乱码导致我一直RE
真是醉了
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define ql(a) memset(a,0,sizeof(a))
#define LL long long
const int UP=50;
const int N=100000-1;
struct node
{
    int val;
    node *child[10];
    node(){val=-1;for(int i=0;i<10;++i) child[i]=NULL;}
}*root;
void ins(char *s,int num)
{
 node *p=root;
 int minn=min(40,(int)strlen(s));
 for(int i=0;i<minn;++i){
    int t=s[i]-0;
    if(p->child[t]==NULL){
        p->child[t]=new node();
    }
    p=p->child[t];
    if(p->val<0) p->val=num;
 }
}
void init()
{
    int f1[65],f2[65],f3[65],r=0;
    ql(f1),ql(f2),ql(f3);
    ins("1",0);
    f1[0]=f1[1]=f2[0]=f2[1]=1;
    for(int i=2;i<=N;++i){ql(f3);r=0;
    int ml=max(f1[0],f2[0]);
        for(int j=1;j<=ml;j++){
            f3[j]=f1[j]+f2[j]+r;
            r=f3[j]/10;
            f3[j]%=10;
            if(j==ml&&r) ml++;
        }f3[0]=ml;
        char s[65]; ql(s);int l=0;
        for(int j=f3[0];j>=1;j--) s[l++]=f3[j]+0;
        for(int j=41;j<=f3[0];j++) s[j-1]=\0;
        ins(s,i);
        ql(f1); for(int j=0;j<=f2[0];j++) f1[j]=f2[j];
        ql(f2); for(int j=0;j<=f3[0];j++) f2[j]=f3[j];
        if(ml>55){
        for(int j=1;j<f1[0];j++) f1[j]=f1[j+1]; f1[f1[0]--]=0;
        for(int j=1;j<f2[0];j++) f2[j]=f2[j+1]; f2[f2[0]--]=0;
        }
    }
}
int Find(char *s)
{
    int len=strlen(s);
if(!strcmp(s,"1")) {return 0;}
node *p=root;
for(int i=0;i<len;++i){
    int t=s[i]-0;
    if(p->child[t]==NULL) return -1;
    p=p->child[t];
}
return p->val;
}
int main()
{
    int k,cas=0;
    char p[55];
    root=new node();
    init();
    cin>>k;
    while(k--){
        scanf("%s",p);
        printf("Case #%d: %d\n",++cas,Find(p));
    }
    return 0;
}

 

HDU 4099 大数+Trie