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. [golang note] 函数定义

    普通函数定义 √ golang函数基本组成:关键字func.函数名.参数列表.返回值.函数体和返回语句. • 语法如下 func 函数名(参数列表) (返回值列表) { // 函数体 } • 示例如下 ...

  2. wget 用法

    wget -r -p -np -k http://xxx.com/xxx

  3. 技术分享会(二):SQLSERVER索引介绍

    SQLSERVER索引介绍 一.SQLSERVER索引类型? 1.聚集索引: 2.非聚集索引: 3.包含索引: 4.列存储索引: 5.无索引(堆表): 二.如何创建索引? 索引示例: 建表 creat ...

  4. java反射子之获取方法信息(二)

    一.获取方法 1.方法作用. 2. 二.获取方法信息.(修饰符,返回值,方法名称,参数列表,抛出的异常). ############################################## ...

  5. Android4.0 Surface机制分析

    1. java层面的Surface     对于Surface我们的认识主要是android的类Surface, android的文档描述Surface是"Handle onto a raw ...

  6. Ubuntu16.04安装MySQLdb

    buntu 系统下进行的操作 首先安装了pip工具 1 sudo apt-get install python-pip 然后使用 1 sudo pip install mysql-python   安 ...

  7. context.Request方法总结

    Request.Params为获取的包含上述两种集合外,还包括当前运行环境变量,COOKIES等的集合.Request.QueryString["param"] getReques ...

  8. PHP递归算法

    /** * 获取菜单 * @param number $id * @return multitype: */ public function menu($id = 0) { $menu = M ( ' ...

  9. Apache httpd服务部署

    1. yum安装 yum install httpd yum install httpd-devel yum install httpd-manual 2. 配置 vim /etc/httpd/con ...

  10. spring boot加mybatis使用Map返回时,当值为空时属性也会没有(转)

    使用spring boot加mybatis时,设置Map返回,当值为空时属性也会没有,就会报错 在application.properties中加入下面配置,将会解决这个问题.   #当查询数据为空时 ...