斐波纳契数+Trie+hdu4099

本博客探讨了一个寻找特定数字开始的最小费波那契数的问题,并提供了使用字母树进行查找的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests

Revenge of Fibonacci

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 204800/204800 K (Java/Others)
Total Submission(s): 2014    Accepted Submission(s): 472


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

这题重点是在蛋疼的大数相加。。。

我们只需要保存前40位插入到字母树中就可以

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100000;
char f[60];
int f1[60],f2[60],tmp[60];
struct node
{
    node *next[10];
    int val;
    node(){memset(next,NULL,sizeof(next));val=-1;}
};
struct TRIE
{
    node *root;
    void clear(){root=new node();}
    int idx(char x){return x-'0';}
    void insert(char *s,int id)
    {
        int n=strlen(s);
        node *p=root;
        for(int i=0;i<n;i++)
        {
            int c=idx(s[i]);
            if(!p->next[c])
                p->next[c]=new node();
            p=p->next[c];
            if(p->val==-1)p->val=id;
            else if(p->val>id)p->val=id;

        }
    }
    int find(char *s)
    {
        int n=strlen(s);
        node *p=root;
        for(int i=0;i<n;i++)
        {
            int c=idx(s[i]);
            if(!p->next[c])return -1;
            p=p->next[c];
        }
        return p->val;
    }
}tree;
void add(int *a,int *b)
{
    for(int i=0;i<60;i++)
        a[i]+=b[i];
    int c=0;
    for(int i=0;i<60;i++)
    {
        a[i]+=c;
        c=0;
        if(a[i]>=10)
        {
            c=a[i]/10;
            a[i]%=10;
        }
    }
    bool flag=true;
    int cnt=0;
    for(int i=59;i>=0;i--)
    {
        if(a[i]==0&&flag)continue;
        flag=false;
        f[cnt++]=a[i]+'0';
        if(cnt>40)break;
    }
    f[cnt]='\0';
}
void change(int *a,int *b)
{
    int pos=0;
    for(int i=59;i>=0;i--)
        if(a[i]!=0){pos=i;break;}
    if(pos>55)
    {
        for(int j=0;j<=59;j++)
            a[j]=a[j+1];
        for(int j=0;j<=59;j++)
            b[j]=b[j+1];
    }
    for(int i=0;i<60;i++)tmp[i]=b[i];
    for(int i=0;i<60;i++)
    {
        b[i]=a[i];
        a[i]=tmp[i];
    }
}
void init()
{
    tree.clear();
    tree.insert("1",0);
    memset(f1,0,sizeof(f1));
    memset(f2,0,sizeof(f2));
    f1[0]=f2[0]=1;
    for(int i=2;i<maxn;i++)
    {
        add(f1,f2);
        change(f1,f2);
        tree.insert(f,i);
    }
}
int main()
{
    int T,cas=1;
    init();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",f);
        printf("Case #%d: %d\n",cas++,tree.find(f));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值