要想通这个题目应该很容易,由于斐波纳契数在近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. get the first and last collection item in Magento

    $product_collection->getFirstItem() $product_collection->getLastItem()

  2. Android消息推送的服务端

    2.Android消息推送 MQTT服务器采用mosquito  http://mosquitto.org/ PHP管理包采用phpmqttclient:https://github.com/toku ...

  3. (转)IOS笔记 #pragma mark的用法

    简单的来说就是为了方便查找和导航代码用的.   下面举例如何快速的定位到我已经标识过的代码.     #pragma mark 播放节拍器 - (void) Run:(NSNumber *)tick{ ...

  4. (转)iOS 开发,工程中混合使用 ARC 和非ARC

    [前提知识] ARC:Automatic Reference Counting,自动引用计数 在开发 iOS 3 以及之前的版本的项目时我们要自己负责使用引用计数来管理内存,比如要手动 retain. ...

  5. P酱的冒险旅途(思维)

    P酱的冒险旅途 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit S ...

  6. STL 源代码剖析 算法 stl_algo.h -- random_shuffle

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie random_shuffle ------------------------------- ...

  7. deferred initcalls与模块化

    deferred initcalls与模块化 有两个技术可以加快kernel的启动速度: 1.deferred initcalls 2.模块化 它们的思想类似,都是将非必要的模块初始化推迟到内核启动之 ...

  8. SpringMVC中的异步提交表单

    1.前言 近期在做一个项目,前台框架用的是EasyUI+SpringMVC,因为对SpringMVC不太了解,所以刚開始接触的时候有点吃力,在此通过一个EasyUi中的DataGrid表格来总结一下. ...

  9. 通过url给action传中文参数乱码解决方案

    比如: http://localhost:8080/projectName/dutyCondition.action?admitstate=0&currentStep=我的博客 传到后台的时候 ...

  10. zoj1013 Great Equipment

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1013 大三了,准备刷一下动态规划,保持思维的灵活性,这一次从zoj刷 ...