Revenge of Fibonacci HDU - 4099(字典树,高精度)

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

题意: 给你菲波那契的前缀,要求找到对应的最小的菲波那契数下标
思路: 只取前70位,对前40位建立字典树。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 2e7 + 7;

char c[205];
int ch[maxn][10],tot,val[maxn];

void add(char *a,char *b,char *back)
{
    int i = (int)strlen(a) - 1,j = (int)strlen(b) - 1;
    int x = 0,y = 0,z = 0,up = 0,cnt = -1;
    while(i >= 0 || j >= 0)
    {
        if(i < 0)x = 0;
        else x = a[i] - '0';

        if(j < 0)y = 0;
        else y = b[j] - '0';

        z = x + y + up;
        c[++cnt] = z % 10 + '0';
        up = z / 10;
        i--;j--;
    }
    if(up > 0)
        c[++cnt] = up + '0';
    for(int i = 0;i <= cnt;i++)
        back[i] = c[cnt - i];
    back[++cnt] = '\0';
}

void insert(char *a,int id)
{
    int len = (int)strlen(a),u = 1;
    for(int i = 0;i < len && i < 41;i++)
    {
        int x = a[i] - '0';
        if(!ch[u][x])
        {
            val[++tot] = id;
            ch[u][x] = tot;
        }
        u = ch[u][x];
    }
}

void init()
{
    tot = 1;
    char a[205],b[205],ans[205];
    a[0] = b[0] = '1';a[1] = b[1] = 0;
    insert(b,0);
    for(int i = 2;i < 100000;++i)
    {
        if(strlen(b) > 70)
        {
            b[strlen(b) - 1] = 0;
            a[strlen(a) - 1] = 0;
        }
        add(a,b,ans);
        insert(ans,i);
        strcpy(a,b);
        strcpy(b,ans);
    }
}
int query(char *a)
{
    int len = (int)strlen(a),u = 1;
    for(int i = 0;i < len;i++)
    {
        int id = a[i] - '0';
        if(!ch[u][id])
            return -1;
        u = ch[u][id];
    }
    return val[u];
}

int main()
{
    init();
    int T;scanf("%d",&T);
    for(int i = 1;i <= T;i++)
    {
        char str[100];scanf("%s",str);
        printf("Case #%d: %d\n",i,query(str));
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值