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 ;
}

最新文章

  1. C#批量插入数据到Sqlserver中的四种方式
  2. 对文本行按特定字段排序(前N个字符或后N个字符),TCPL 练习5-17
  3. 39 网络相关函数(七)——live555源码阅读(四)网络
  4. vi/vim初步接触
  5. Spring计划
  6. Python内部类型
  7. Eclipse去除jquery引入错误
  8. 23讲 URL
  9. WINDOWS下简单操作SQLITE3
  10. ASP.NET应用程序和ASP.NET网站所共有的文件: App_Browsers 等
  11. 棋盘 chess
  12. 【洛谷P3014】Cow Line
  13. 更好用的cmd窗口
  14. mycat中schema.xml的一些解释
  15. 20155204《网络对抗》Exp 6 信息搜集与漏洞扫描
  16. andrdoi示例项目SampleSyncAdapter分析
  17. zw版【转发&#183;台湾nvp系列Delphi例程】HALCON ConvolImage
  18. Delphi笔记-自定义提示窗口
  19. Python 中的那些坑总结——持续更新
  20. PHP逻辑运算符中的and和&amp;&amp;以及or和||是有区别的

热门文章

  1. java通过url抓取网页数据
  2. Jmeter数据库mysql测试说明
  3. Oracle数据库创建表ID字段的自动递增
  4. C#打开文件资源管理器
  5. 20145302张薇《Java程序设计》实验二报告
  6. 20145335郝昊《Java程序设计》第2周学习总结
  7. mongodb入门很简单(1)
  8. 如何退出minicom【学习笔记】
  9. 混合开发的大趋势之一React Native之简单的登录界面
  10. Vue中使用百度地图——设置地图标注