TOJ 3820 Revenge of Fibonacci(大数+trie)

描述

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.

输入

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.

输出

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.

样例输入

15
1
12
123
1234
12345
9
98
987
9876
98765
89
32
51075176167176176176
347746739
5610

样例输出

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

题意

求最小第几个斐波那契数前缀等于这个数

题解

看到这种查询多又是前缀的很容易想到预处理+trie

预处理硬算再取前40位很明显会TLE,位数太多了,只取40位会发现精度不够,为了精确取到了前60位,和暴力打表对上

查询前缀,直接插入到trie然后查询就行了

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 //trie
 5 const int maxn=5e6+5;
 6 int cnt,ch[maxn][10],val[maxn];
 7 int getIdx(char a){return a-‘0‘;}
 8 void insert(char st[],int d){
 9     int u=0,l=strlen(st);
10     for(int i=0;i<l&&i<41;i++){
11         int k=getIdx(st[i]);
12         if(!ch[u][k]){
13             val[cnt]=d;
14             ch[u][k]=cnt++;
15             memset(ch[cnt],0,sizeof ch[cnt]);
16         }
17         u=ch[u][k];
18     }
19 }
20 int query(char st[]){
21     int u=0,l=strlen(st);
22     for(int i=0;i<l;i++){
23         int k=getIdx(st[i]);
24         if(!ch[u][k])return -1;
25         u=ch[u][k];
26     }
27     return val[u];
28 }
29
30 char c[100],str[100];
31 void add(char a[],char b[],char back[])
32 {
33     int x,y,z,i=strlen(a)-1,j=strlen(b)-1,k=0,p=0;
34     while(i>=0||j>=0)
35     {
36         if(i<0)x=0;
37         else x=a[i]-‘0‘;
38         if(j<0)y=0;
39         else y=b[j]-‘0‘;
40         z=x+y+p;
41         c[k++]=z%10+‘0‘;
42         p=z/10;
43         i--,j--;
44     }
45     if(p>0)c[k++]=p+‘0‘;
46     for(i=0;i<k;i++)back[i]=c[k-1-i];
47     back[k]=‘\0‘;
48 }
49 void init()
50 {
51     cnt=1;
52     memset(ch[0],0,sizeof ch[0]);
53     memset(val,0x3f3f3f3f,sizeof val);
54     char a[100],b[100],ans[100];
55     a[0]=‘1‘,a[1]=0;
56     b[0]=‘1‘,b[1]=0;
57     insert(a,0);
58     for(int i=2;i<100000;i++)
59     {
60         if(strlen(b)>60)a[strlen(a)-1]=0,b[strlen(b)-1]=0;
61         add(a,b,ans);
62         insert(ans,i);
63         strcpy(a,b);
64         strcpy(b,ans);
65     }
66 }
67 int main(){
68     init();
69     int t,T=1;
70     scanf("%d",&t);
71     while(t--)
72     {
73         scanf("%s",str);
74         printf("Case #%d: %d\n",T++,query(str));
75     }
76     return 0;
77 }

原文地址:https://www.cnblogs.com/taozi1115402474/p/10072648.html

时间: 2024-10-22 06:33:49

TOJ 3820 Revenge of Fibonacci(大数+trie)的相关文章

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

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

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)

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

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 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): 2027    Accepted Submission(s): 475 Problem Description The well-known Fibonacci sequence is defined as following: Here w

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

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