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 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
debug三天竟是数据范围看错的惨案,将<=10w改成<10w后AC   = =
还有一点关于大数模拟,一开始string一直错我以为是这个问题就改成手打的模拟大数加法,
关于这个char数组并不会自动初始化   假如  char s[35];  for(int i=0;i<10;++i) s[i]=i+'0'; 就会出现乱码导致我一直RE
真是醉了
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define ql(a) memset(a,0,sizeof(a))
#define LL long long
const int UP=;
const int N=-;
struct node
{
int val;
node *child[];
node(){val=-;for(int i=;i<;++i) child[i]=NULL;}
}*root;
void ins(char *s,int num)
{
node *p=root;
int minn=min(,(int)strlen(s));
for(int i=;i<minn;++i){
int t=s[i]-'';
if(p->child[t]==NULL){
p->child[t]=new node();
}
p=p->child[t];
if(p->val<) p->val=num;
}
}
void init()
{
int f1[],f2[],f3[],r=;
ql(f1),ql(f2),ql(f3);
ins("",);
f1[]=f1[]=f2[]=f2[]=;
for(int i=;i<=N;++i){ql(f3);r=;
int ml=max(f1[],f2[]);
for(int j=;j<=ml;j++){
f3[j]=f1[j]+f2[j]+r;
r=f3[j]/;
f3[j]%=;
if(j==ml&&r) ml++;
}f3[]=ml;
char s[]; ql(s);int l=;
for(int j=f3[];j>=;j--) s[l++]=f3[j]+'';
for(int j=;j<=f3[];j++) s[j-]='\0';
ins(s,i);
ql(f1); for(int j=;j<=f2[];j++) f1[j]=f2[j];
ql(f2); for(int j=;j<=f3[];j++) f2[j]=f3[j];
if(ml>){
for(int j=;j<f1[];j++) f1[j]=f1[j+]; f1[f1[]--]=;
for(int j=;j<f2[];j++) f2[j]=f2[j+]; f2[f2[]--]=;
}
}
}
int Find(char *s)
{
int len=strlen(s);
if(!strcmp(s,"")) {return ;}
node *p=root;
for(int i=;i<len;++i){
int t=s[i]-'';
if(p->child[t]==NULL) return -;
p=p->child[t];
}
return p->val;
}
int main()
{
int k,cas=;
char p[];
root=new node();
init();
cin>>k;
while(k--){
scanf("%s",p);
printf("Case #%d: %d\n",++cas,Find(p));
}
return ;
}

HDU 4099 大数+Trie的更多相关文章

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

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

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

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

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

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

  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 (数学+字典数)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4099 这个题目就是一个坑或. 题意:给你不超过40的一串数字,问你这串数字是Fibonacci多少的开头 ...

  6. hdu 1002大数(Java)

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. hdu 5047 大数找规律

    http://acm.hdu.edu.cn/showproblem.php?pid=5047 找规律 信kuangbin,能AC #include <stdio.h> #include & ...

  8. hdu 5050 大数

    http://acm.hdu.edu.cn/showproblem.php?pid=5050 大数模板最大公约数 信kuangbin,能AC #include <cstdio> #incl ...

  9. (字典树)Revenge of Fibonacci -- HDU -- 4099

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4099 要用c++交哦, G++ MLE 不是很懂,先粘上慢慢学习 代码: #include<std ...

随机推荐

  1. 使用cygwin移植Linux的项目到Windows下之总结(转)

    使用cygwin移植Linux的项目到Windows下之总结(转) 原文 http://my.oschina.net/michaelyuanyuan/blog/68615?p=1   一.why   ...

  2. git安装教程(windows安装)

    git下载地址 https://git-scm.com/download/win 选择安装的组件,推荐全选 Additional icons 附加图标 ​ On the Desktop 在桌面上 Wi ...

  3. Git-创建和合并分支

    本人拜读了廖雪峰老师关于Git的讲述后整理所得 分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你 ...

  4. 优化css,增加性能

    转载自:https://www.cnblogs.com/xiaoloulan/p/5801663.html 前端性能优化一直是一个比较热门的话题,我们总是在尽我们最大的努力去,提高我们的页面性能,比如 ...

  5. C++切割字符串

    std::string text = "2001_1;2005_5;"; std::stringstream ss(text); std::string sub_str; std: ...

  6. mongodb中的_id的ObjectId的生成规则

    MongoDB中存储的文档必须有一个"_id" .这个键值可以是任何类型,默认是ObjectID对象.在一个集合里,每个文档都有一个唯一的“_id”,确保集合里的每个文档都能被唯一 ...

  7. JavaWeb 如何在web.xml中配置多个servlet

    15:34:42 <servlet> <description></description> <display-name>ListMusicServle ...

  8. java泛型的实现原理

    java泛型的实现原理是类型擦除.Java的泛型是伪泛型.在编译期间,所有的泛型信息都会被擦除掉.   Java中的泛型基本上都是在编译器这个层次来实现的.在生成的Java字节码中是不包含泛型中的类型 ...

  9. ios 常见问题解决 以及小技巧

    1.使用cocoaPods引用第三方类库,报错:file not found   . 解决方案:设置 Project->Info->Configurations之后  clear ,然后再 ...

  10. JS正则表达式从入门到入土(10)—— 字符串对象方法

    字符串对象方法 search方法 String.prototype.search(reg) search方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串,方法返回第一个匹配结果的 ...