HDU4099-Revenge of Fibonacci(trie树+数学基础)
Revenge of Fibonacci
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/Others)
Total Submission(s): 1944 Accepted Submission(s): 446
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.
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.
– you think what Fibonacci wants to told you beyonds your ability.
15
1
12
123
1234
12345
9
98
987
9876
98765
89
32
51075176167176176176
347746739
5610
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 7000000;
const int INF = 1e8;
int ch[maxn][10];
int val[maxn];
int cnt;
char c[200];
char str[200];
void add(char a[],char b[],char back[]){ int i=strlen(a)-1,j=strlen(b)-1,k=0;
int x,y,z;
int up=0;
while(i>=0||j>=0)
{
if(i<0)x=0;
else x=a[i]-'0';
if(j<0)y=0;
else y=b[j]-'0';
z=x+y+up;
c[k++]=z%10+'0';
up=z/10;
i--;
j--;
}
if(up>0)c[k++]=up+'0';
for(i=0;i<k;i++)back[i]=c[k-1-i];
back[k]='\0';
}
int getIdx(char a){
return a-'0';
}
void insert(char st[],int d){
int u = 0;
for(int i = 0; i < strlen(st) && i < 42; i++){
int k = getIdx(st[i]);
if(!ch[u][k]){
val[cnt] = d;
ch[u][k] = cnt++;
memset(ch[cnt],0,sizeof ch[cnt]);
}
u = ch[u][k];
}
}
int query(char st[]){
int u = 0;
for(int i = 0; i < strlen(st); i++){
int k = getIdx(st[i]);
if(!ch[u][k]){
return -1;
}
u = ch[u][k];
}
return val[u];
}
void init(){
cnt = 1;
memset(ch[0],0,sizeof ch[0]);
for(int i = 0; i < maxn; i++)
val[i] = INF;
char a[200],b[200],ans[200];
a[0] = '1',a[1] = 0;
b[0] = '1',b[1] = 0;
insert(a,0);
for(int i = 2; i < 100000; i++){
if(strlen(b) > 70){
a[strlen(a)-1] = 0;
b[strlen(b)-1] = 0;
}
add(a,b,ans);
insert(ans,i);
strcpy(a,b);
strcpy(b,ans);
}
}
int main(){
init();
int ncase,T=1;
cin >> ncase;
while(ncase--){
cin >> str;
printf("Case #%d: %d\n",T++,query(str));
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
HDU4099-Revenge of Fibonacci(trie树+数学基础)的更多相关文章
- hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法
Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...
- hdu4099 Revenge of Fibonacci 字典树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4099 思想很容易想到 就是预处理出前10w个的fib数,然后建树查询 建树时只用前40位即可,所以在计 ...
- HDU4099 Revenge of Fibonacci(高精度+Trie)
Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/ ...
- HDU 4099 Revenge of Fibonacci Trie+高精度
Revenge of Fibonacci Problem Description The well-known Fibonacci sequence is defined as following: ...
- hdu 4099 Revenge of Fibonacci 字典树+大数
将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...
- hdu4099 Revenge of Fibonacci
题意:给定fibonacci数列,输入前缀,求出下标.题目中fibonacci数量达到100000,而题目输入的前缀顶多为40位数字,这说明我们只需要精确计算fibinacci数前40位即可.查询时使 ...
- UVa 12333 Revenge of Fibonacci (字典树+大数)
题意:给定一个长度小于40的序列,问你那是Fib数列的哪一项的前缀. 析:首先用大数把Fib数列的前100000-1项算出来,注意,一定不能是100000,要不然会WA的,然后每个数取前40位,不足4 ...
- hdu 4099 Revenge of Fibonacci 大数+压位+trie
最近手感有点差,所以做点水题来锻炼一下信心. 下周的南京区域赛估计就是我的退役赛了,bless all. Revenge of Fibonacci Time Limit: 10000/5000 MS ...
- UVA 12333 Revenge of Fibonacci
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- leetcode笔记:Remove Duplicates from Sorted Array II
一.题目描写叙述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是相似的.仅仅只是这里同意出现反复的数字而已,能够採用二分搜索的变种算法.仅仅只是增 ...
- 批量杀死MySQL连接的几种方法
法一: 通过information_schema.processlist表中的连接信息生成需要处理掉的MySQL连接的语句临时文件,然后执行临时文件中生成的指令. mysql> select ...
- form表单上传附件的几种方法
问题描述:在网页开发过程中,当需要上传附件(图片,音频,视频等)时,常规方法是使用form表单进行提交,这里总结一下form表单提交的几种方法. 参考地址:http://www.cnblogs.com ...
- deep learning实践经验总结
近期拿caffe来做图片分类.遇到不少问题,同一时候也吸取不少教训和获得不少经验. 先看样例再总结经验. 这是一个2类分类器.分的是条纹衣服和纯色衣服. 先看几张图片. 条纹衣服: 纯色衣服: w ...
- [Angular] Using ngOnChanges lifeCycle hook to break object reference
What could be the issue, for example we have two list: Parent component: @Component({ selector: 'pas ...
- Indy10 控件的使用(2)TidTCpServer组件学习
以下来自英文原版帮助文件,文桓英语不好,翻译了老半天.有错误的地方见谅,别骂我. TIdTCPServer = class(TIdComponent) Description TIdTCPServer ...
- iOS中js与objective-c的简单交互
1.首先是objective-c调用js中的代码,可以用UIWebview中的一个方法 stringByEvaluatingJavaScriptFromString:后面接的是js中的方法名.这个函数 ...
- Notes on OpenSSL and Qt(ssl.pri,qsslocket_openssl_symbols.cpp)
Libraries name of openssl? The "library" portion of OpenSSL consists of two libraries. On ...
- callback回调函数理解 相当于this指针
1.callback函数在微软的官方手册中是这样定义callback函数的:“callback函数是由应用程序定义而由操作系统调用的函数”. 凡是由用户设计而却由windows系统调用的函数,统称 ...
- tombstone问题分析
tombstone文件包含了发生问题的进程ID信息 I/DEBUG ( 241): pid: 244, tid: 244, name: mediaserver >>> /system ...