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

题目大意:
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树+数学基础)的更多相关文章

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

    Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...

  2. hdu4099 Revenge of Fibonacci 字典树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4099 思想很容易想到 就是预处理出前10w个的fib数,然后建树查询 建树时只用前40位即可,所以在计 ...

  3. HDU4099 Revenge of Fibonacci(高精度+Trie)

    Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 204800/204800 K (Java/ ...

  4. HDU 4099 Revenge of Fibonacci Trie+高精度

    Revenge of Fibonacci Problem Description The well-known Fibonacci sequence is defined as following: ...

  5. hdu 4099 Revenge of Fibonacci 字典树+大数

    将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...

  6. hdu4099 Revenge of Fibonacci

    题意:给定fibonacci数列,输入前缀,求出下标.题目中fibonacci数量达到100000,而题目输入的前缀顶多为40位数字,这说明我们只需要精确计算fibinacci数前40位即可.查询时使 ...

  7. UVa 12333 Revenge of Fibonacci (字典树+大数)

    题意:给定一个长度小于40的序列,问你那是Fib数列的哪一项的前缀. 析:首先用大数把Fib数列的前100000-1项算出来,注意,一定不能是100000,要不然会WA的,然后每个数取前40位,不足4 ...

  8. hdu 4099 Revenge of Fibonacci 大数+压位+trie

    最近手感有点差,所以做点水题来锻炼一下信心. 下周的南京区域赛估计就是我的退役赛了,bless all. Revenge of Fibonacci Time Limit: 10000/5000 MS ...

  9. UVA 12333 Revenge of Fibonacci

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. [Immutable.js] Updating nested values with ImmutableJS

    The key to being productive with Immutable JS is understanding how to update values that are nested. ...

  2. C++基础学习教程(七)----类编写及类的两个特性解析---&gt;多态&amp;继承

    类引入 到眼下为止我们所写的自己定义类型都是keywordstruct,从如今起我们将採用class方式定义类,这样的方式对于学习过其它高级语言包含脚本(Such as Python)的人来说再熟悉只 ...

  3. CentOS 7 下使用yum安装MySQL5.7.20 最简单 图文详解

      原文地址:https://blog.csdn.net/z13615480737/article/details/78906598  CentOS7默认数据库是mariadb, 但是 好多用的都是m ...

  4. jquery-ajax、struts2、json数据问题

    jquery代码: $.ajax({ url:url, type:'post', data:{"key1": "value1", "key2" ...

  5. ios开发网络学习十二:NSURLSession实现文件上传

    #import "ViewController.h" // ----WebKitFormBoundaryvMI3CAV0sGUtL8tr #define Kboundary @&q ...

  6. 在shell脚本中调用sqlplus 分类: H2_ORACLE 2013-06-23 13:01 1437人阅读 评论(0) 收藏

    #!/bin/bash sqlplus dc_file_data_js/dc_file_data_js << EOF1 set linesize 500; set pagesize 100 ...

  7. pandas 学习(四)—— 数据处理(清洗)、缺失值的处理

    创建 DataFrame: df = pd.DataFrame(np.random.randint(0, 10, (2, 4)), columns=list('ABCD')) 0. 为 data fr ...

  8. 【p091】多项式输出

    一元 n 次多项式可用如下的表达式表示: 其中,aixi 称为i次项,ai称为i次项的系数.给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式: 多项式中自变量为x,从左到右按照 ...

  9. php课程 5-18 数组排序和合并拆分函数有哪些

    php课程  5-18   数组排序和合并拆分函数有哪些 一.总结 一句话总结:分类来记.这些函数自己都可以写,费点时间而已. 1.array_combine()和array_merge()的区别是什 ...

  10. php实现二叉树遍历

    php实现二叉树遍历 一.总结 关注输入输出 二.php实现二叉树遍历 题目描述 编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储). 例如如下的先序遍历字符串 ...