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. iOS 定位方式 iOSNsPredicateString 详解

    原文地址https://segmentfault.com/a/1190000010205649 前言 由于使用id.className.AccessibilityId定位方式较为简单,多数情况下,在同 ...

  2. co.js异步回调原理理解

    co.js是基于es6的generator实现的,相当于generator函数的一个自动执行器 generator的简单介绍 function* fn(){ before() yield firstY ...

  3. Bitmap: a C++ class

    Bitmap: a C++ class        The five steps involved to draw a bitmap: Load bitmap using LoadBitmap or ...

  4. Magento 本地搬家至网络服务器步骤

    1.将本地的Magento的数据库备份下来. 2.将本地的Magento网站资料做成ZIP资料. 3.将Magento网站 ZIP资料上传到服务器的域名指向的资料夹内. 4.将ZIP解压出来,移动到域 ...

  5. 基于HTML5 FileSystem API的使用介绍(转)

    FileSystem提供了文件夹和文件的创建.移动.删除等操作,大大方便了数据的本地处理, 而且所有的数据都是在沙盒(sandboxed)中,不同的web程序不能互相访问,这就保证了数据 的完整和安全 ...

  6. Maven详解(转)

    原文出自: http://www.cnblogs.com/hongwz/p/5456578.html http://ifeve.com/maven-1/ Maven介绍: Maven是一个强大的Jav ...

  7. 2016-2017 National Taiwan University World Final Team Selection Contest C - Crazy Dreamoon

    题目:Statements Dreamoon likes algorithm competitions very much. But when he feels crazy because he ca ...

  8. ARKit 研究笔记一

    软件需求:Xcode9.x .blender 硬件需求:iphone 6s + 系统:iOS 11 + 技能储备: ARKit .SceneKit(苹果提供的3d游戏库) 或 SpriteKit(苹果 ...

  9. Uninstalling JIRA applications from Linux

     If you wish to re-install JIRA in 'unattended mode', do not uninstall your previous installation of ...

  10. 读写文件时0A转化为0D 0A

    转自:http://www.cnblogs.com/congdiaodiao/p/4529785.html 用C++写东西,需要往文件里写数据,很简单的代码,大概是这个样子: #include < ...