Hrbust 1209/hdu 4099 Revenge of Fibonacci "dictionary tree + large number"

Source: Internet
Author: User

Revenge of Fibonacci
Time limit:5000 MS Memory limit:204800 K
Total submit:37(users) Total accepted:18(users) Rating: Special Judge: No
Description

The well-known Fibonacci sequence is defined as following:

F (0) = f (1) = 1F (n) = f (n-1) + f (n-2) (n >= 2)


Here we regard N as the index of the Fibonacci number F (n).
This sequence have been studied since the publication of Fibonacci ' s book Liber Abaci. So far, many properties of this sequence has been introduced.
You had been interested in this sequence and while after reading lots of papers about it. You think there's no need to the it anymore because of the lack of its unrevealed properties. Yesterday, you decided to study some and other sequences like Lucas sequence instead.
Fibonacci came into your dream last night. "Stupid human beings. Lots of important properties of Fibonacci sequence has 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. You decided to the write a program to the find this number out of order to continue your the Fibonacci sequence.
Input
There is 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, the there is a single line containing one non-empty string made up of the at most of the digits. And there won ' t is any unnecessary leading zeroes.
Output
For each test case, output the smallest index of the smallest Fibonacci number whose decimal notation begins with the give n digits. If no Fibonacci number with index smaller than 100000 satisfy this 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
Asia Shanghai Regional Contest
Recommend
Zidaratu

Title: Give a string of not more than 40 length, indicating the prefix of a Fibonacci sequence, if a Fibonacci number with such a prefix is found in the 100,000 Fibonacci numbers, output its number.


Analysis: Because it is a maintenance prefix, if the length is too long, we need to remove the number from the single digit. Each Fibonacci prefix is added to the dictionary tree and then entered in each query.


AC Code: (Thanks

? Alpha?? Shan
Seniors ' code 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-9int Now;//now indicates number}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;//the number of nodes. 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); }}








Hrbust 1209/hdu 4099 Revenge of Fibonacci "dictionary tree + large number"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.