要想通这个题目应该很容易,由于斐波纳契数在近100项之后很大,早就超出long long了。而输入最长的序列才40个数字,所以大约保留前50位,前40位是没有误差的!!!其实,想想我们判断double数字相等时fabs(a-b)<1e-10来忽略double数字由于加法带来的误差,道理是一样的,甚至C++默认的还是1e-5。而保险起见,我的方法中保留了100位。当然这个保留50位的数字加法,也只能模拟一遍,因为要计算10^5项。

  然而测试样例有50000组,所以我们先要对输入的字符串做一个字典树!虽然我没学过也没听过,后来AC了队友跟我说这个叫字典树,写个博客纪念一下。

  YY一下,我弹了近8次,前3次因为没有建字典树超时,后5次竟然是因为第0项也是计入的,斐波那契序列为1,1,2,3,5,8……。而我竟然认为第一个1不算的。擦擦,其实样例都没过,我一直以为过了,然后还找不到思路错哪了。我是有多眼瞎啊!!!今年还有最后一次机会,希望比赛拿点成绩,千万别再坑这里了。

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxlen=;
const int maxn=maxlen+;
char target[];
int active_id,active_idx;
void ccplus(char *&ch,int &cl,char *&sh,int &sl){
if(cl < sl) ch--,cl++, ch[]='';
char carry=; int idx=cl;
while(idx--){
ch[idx] += sh[idx]+carry-'';
if(ch[idx] > '') ch[idx]-=, carry=;
else carry=;
}
if(carry) ch--,cl++, ch[]='';
if(cl > ) cl--, sl--;
ch[cl]='\0';
//if(active_idx <= 25) printf("active_idx = %d, ch = %s, cl = %d\n",active_idx,ch,cl);
swap(ch,sh); swap(cl,sl);
}
char f1[maxn],f2[maxn];
char *ch,*sh;
int cl,sl;
const int maxt=;
struct tree{
int id;
int next[];
void init(int i=-){ id=i; for(int j=;j<;j++) next[j]=-; }
}tr[maxt*];
int tcnt=;
int value[maxt],idnext[maxt];
void make_tree(int idx,char *tt){
//printf("idx = %d, tt[1] = %s\n",idx,tt+1); //char cc; cin>>cc;
if(tt[]=='\0'){
if(tr[idx].id <= ) tr[idx].id = active_id;
else {
int t = tr[idx].id;
while(idnext[t] > ) t = idnext[t];
idnext[t] = active_id;
}
return;
}
int ne=tt[]-'';
//if(ne < 0) cout<<"fuck ne = "<<ne<<endl;
if(tr[idx].next[ne] <= )
tr[idx].next[ne]=tcnt, tr[tcnt].init(), tcnt++;
make_tree(tr[idx].next[ne],tt+);
}
void search_tree(int idx,char *tt){
int id=tr[idx].id;
if(id > && value[id] < ) {
value[id]=active_idx;
while(idnext[id] > ) id=idnext[id], value[id]=active_idx;
}
int ne=tt[]-'';
if(tt[] == '\0' || tr[idx].next[ne] <= ) return;
search_tree(tr[idx].next[ne],tt+);
}
int main()
{
int cases; cin>>cases;
memset(idnext,-,sizeof(idnext));
memset(value,-,sizeof(value));
tr[].init();
for(int cas=;cas<=cases;cas++) {
scanf("%s",target+);
active_id = cas;
make_tree(,target);
//cout<<"case "<<cas<<" is over!"<<endl;
}
ch=&f1[maxn-],sh=&f2[maxn-];
ch[]='', ch[]='\0';
sh[]='', sh[]='\0';
cl=sl=;
active_idx=; search_tree(,ch-);
active_idx=; search_tree(,ch-);
active_idx=; search_tree(,sh-);
active_idx=;
while(active_idx < maxlen)
ccplus(ch,cl,sh,sl), search_tree(,sh-), active_idx++;
for(int cas=;cas<=cases;cas++)
printf("Case #%d: %d\n",cas,value[cas]);
return ;
}

  

hdu4099的更多相关文章

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

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

  2. HDU4099(斐波那契数列与字典树)

    题目:Revenge of Fibonacci 题意:给出斐波那契数列的前k位,k不超过40,找出最小的正整数n,满足F(n)的前k位与给定数的前k位相同,斐波那契数列的项数不超过100000. 解析 ...

  3. hdu4099 Revenge of Fibonacci 字典树

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

  4. HDU--4099

    题目: Revenge of Fibonacci 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4099 分析:字典树的应用.在求Fibonacci的前 ...

  5. hdu4099 Revenge of Fibonacci

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

  6. hdoj4099(字典树+高精度)

    题目链接:https://vjudge.net/problem/HDU-4099 题意:给T组询问,每个询问为一个字符串(长度<=40),求以该字符串为开始的fibonacci数列的第一个元素的 ...

随机推荐

  1. tky项目第①个半月总结

    增加tky项目开发组已经有半个月了,这半个月一直是伴随着加班度过,学习了不少东西,也有不少抱怨,这些都是宝贵的工作经验体会.有必要在此好好总结一下. 我是中途加进这个项目的.也就是说.组内其它人员已经 ...

  2. C/C++输入输出

    一. cin>>当碰到空格或换行符'\n'时,输入结束 该操作符是根据后面变量的类型读取数据. 输入结束条件 :遇到Enter.Space.Tab键. 对结束符的处理 :丢弃缓冲区中使得输 ...

  3. shared_ptr智能指针源码剖析

    (shared_ptr)的引用计数本身是安全且无锁的,但对象的读写则不是,因为 shared_ptr 有两个数据成员,读写操作不能原子化.根据文档 (http://www.boost.org/doc/ ...

  4. Android JSON数据的读取和创建

    预先准备好的一段JSON数据 { "languages":[ {"id":1,"ide":"Eclipse"," ...

  5. sass教程

    sass教程 1. 使用变量; sass让人们受益的一个重要特性就是它为css引入了变量.你可以把反复使用的css属性值 定义成变量,然后通过变量名来引用它们,而无需重复书写这一属性值.或者,对于仅使 ...

  6. Header() in PHP &html – Refresh (Redirect) to Location (URL) in X seconds

    Case 1 : Redirect a page to a URL without waiting in PHP. 1 header("Location: index.php"); ...

  7. KETTLE使用入门

    一.准备文件 1.安装java虚拟机 2.安装kettle安装文件 二.使用步骤 1.点击Spoon.bat,启动kettle,弹出DOS窗口如下: 2.进入主界面 3.新建资源库

  8. log实例

    配置详解见2014.10月篇 log4j的pom.xml <dependency> <groupId>log4j</groupId> <artifactId& ...

  9. C语言 格式说明符

    整数 lL代表long %#0 和%#x 可打印出八进制和十六进制前缀 short int long long long 无符号 八进制 %ho %o %Lo %LLo 十进制 %hu %u %Lu ...

  10. impala安装

    http://blog.sina.com.cn/s/blog_8c6d7ff60101e3lh.html ----------------------------------------------- ...