HDU4099-Revenge of Fibonacci(trie树+数学基础)

Revenge of Fibonacci

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 204800/204800 K (Java/Others)

Total Submission(s): 1944    Accepted Submission(s): 446

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

题目大意:

T组测试样例,问你一个数字串是哪个斐波那契数列的前缀,要求下标要最小

做法:

先算斐波那契数,因为数字较大,所以要用大数模板,考虑到询问的数字串最多为40个,所以在插入trie树时可以选择插入<=40个,这样可以节省很大的内存。

大数模板网上找的。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 7000000;
const int INF = 1e8;
int ch[maxn][10];
int val[maxn];
int cnt;
char c[200];
char str[200];
void add(char a[],char b[],char back[]){

    int i=strlen(a)-1,j=strlen(b)-1,k=0;
    int x,y,z;
    int up=0;
    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[k++]=z%10+'0';
        up=z/10;
        i--;
        j--;
    }
    if(up>0)c[k++]=up+'0';
    for(i=0;i<k;i++)back[i]=c[k-1-i];
    back[k]='\0';
}
int getIdx(char  a){
    return a-'0';
}
void insert(char st[],int d){
    int u = 0;
    for(int i = 0; i < strlen(st) && i < 42; i++){
        int k = getIdx(st[i]);
        if(!ch[u][k]){
            val[cnt] = d;
            ch[u][k] = cnt++;
            memset(ch[cnt],0,sizeof ch[cnt]);
        }
        u = ch[u][k];
    }
}
int query(char st[]){
    int u = 0;
    for(int i = 0; i < strlen(st); i++){
        int k = getIdx(st[i]);
        if(!ch[u][k]){
            return -1;
        }
        u = ch[u][k];
    }
    return val[u];
}
void init(){
    cnt = 1;
    memset(ch[0],0,sizeof ch[0]);
    for(int i = 0; i < maxn; i++)
        val[i] = INF;
    char a[200],b[200],ans[200];
    a[0] = '1',a[1] = 0;
    b[0] = '1',b[1] = 0;
    insert(a,0);
    for(int i = 2; i < 100000; i++){
        if(strlen(b) > 70){
            a[strlen(a)-1] = 0;
            b[strlen(b)-1] = 0;
        }
        add(a,b,ans);
        insert(ans,i);
        strcpy(a,b);
        strcpy(b,ans);
    }
}
int main(){
    init();
    int ncase,T=1;
    cin >> ncase;
    while(ncase--){
        cin >> str;
        printf("Case #%d: %d\n",T++,query(str));
    }
    return 0;
}

HDU4099-Revenge of Fibonacci(trie树+数学基础)

时间: 2025-01-13 15:54:30

HDU4099-Revenge of Fibonacci(trie树+数学基础)的相关文章

hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法

Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输出-1: 思路:直接使用数组模拟加法,再用Trie树插入查找即可:但是一般使用new Trie()的代码都是MLE的.反而我之前写的,直接得到数组大小的maxnode版本的内存可以接受:并且还有一点就是前40位的精度问题:由于是自己计算出来的finboncci数列,并不是系统给的,所以1的进位不会形成递推

HDU4099 Revenge of Fibonacci(高精度+Trie)

Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 204800/204800 K (Java/Others) Total Submission(s): 2582    Accepted Submission(s): 647 Problem Description The well-known Fibonacci sequence is defined as following: Here w

hdu 4099 Revenge of Fibonacci(字典树)

Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 204800/204800 K (Java/Others) Total Submission(s): 2355    Accepted Submission(s): 587 Problem Description The well-known Fibonacci sequence is defined as following: Here w

HDU 4099 Revenge of Fibonacci Trie+高精度

Revenge of Fibonacci 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

hdu4099 Revenge of Fibonacci

题意:给定fibonacci数列,输入前缀,求出下标.题目中fibonacci数量达到100000,而题目输入的前缀顶多为40位数字,这说明我们只需要精确计算fibinacci数前40位即可.查询时使用字典树.在计算时,为了保证前40位精确无误.在此我计算了前60位.以保证前面不在进位. 注意点: 1)关于表示进位问题,需要控制计算的数位数在60以内,切记计算时不要错位(相同位要对齐). 2)坑点:题目给出的数插入字典树最好插入前40位即可,否则MLE. 3)坑点:题目只要求计算下标不超过100

HDOJ 题目1789 Revenge of Fibonacci(大数, 字典树)

Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 204800/204800 K (Java/Others) Total Submission(s): 2405    Accepted Submission(s): 609 Problem Description The well-known Fibonacci sequence is defined as following: Here w

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)

HDU 4099 Revenge of Fibonacci

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 w

poj3630 Phone List (trie树模板题)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26328   Accepted: 7938 Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogu