题意:给定fibonacci数列,输入前缀,求出下标。题目中fibonacci数量达到100000,而题目输入的前缀顶多为40位数字,这说明我们只需要精确计算fibinacci数前40位即可。查询时使用字典树。在计算时,为了保证前40位精确无误。在此我计算了前60位。以保证前面不在进位。

注意点:

1)关于表示进位问题,需要控制计算的数位数在60以内,切记计算时不要错位(相同位要对齐)。

2)坑点:题目给出的数插入字典树最好插入前40位即可,否则MLE.

3)坑点:题目只要求计算下标不超过100000,计算到100000或者以上均会WA,因为输出值不是-1

下面是数组的AC代码:824ms

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<string>
#include<map>
#include<fstream>
#include<ctime>
#include<queue>
#include<vector>
#include<numeric>
#include<string.h>
#include<iomanip>
#include<sstream>
#include<algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef vector<int> BigInterger;
const int maxn = 100000;
const int maxsize = 10;
const int maxnum =65; struct TrieNode{
TrieNode(int d = -1) :id(d){ memset(next, NULL, sizeof(next)); }
int id;
TrieNode*next[maxsize];
};
TrieNode*T = new TrieNode; void insert(char*str,const int&index){
int len = strlen(str);
TrieNode*p = T;
int t = 0;
for (int i = len-1; i>=0&&t<40;i--,t++){
int id =str[i]-'0';
if (!p->next[id])p->next[id] = new TrieNode(index);
p = p->next[id];
}
} int search(char*str){
TrieNode*p = T;
while (*str != '\0'){
int id = *str++ - '0';
if (!p->next[id])return -1;
p = p->next[id];
}
return p->id;
}
void init(){
char f[3][maxnum];
f[1][0] ='1'; f[1][1] = '\0';
f[0][0] ='1'; f[0][1] = '\0';
insert("1", 0);
for (int i = 2; i<maxn; i++){
int g = 0,j=0,cnt = 0, x;
int r1 = (i - 1) % 3, r2 = (i - 2) % 3, r = i % 3;
int lena = strlen(f[r1]);
int lenb = strlen(f[r2]);
if (lena >= maxnum - 5){
memcpy(f[r1], f[r1]+1, lena - 1); f[r1][lena - 1] = '\0';
memcpy(f[r2], f[r2]+1, lenb - 1); f[r2][lenb - 1] = '\0';
lena--, lenb--;
}
while (g || j < lena||j<lenb){
x = g;
if (j < lena)x += f[r1][j]-'0';
if (j < lenb)x += f[r2][j]-'0';
f[r][cnt++] = x % 10+'0';
g = x / 10;
j++;
}
f[r][cnt] = '\0';
insert(f[r], i);
}
}
int main(){
init();
int T,kase=1;
char str[50];
scanf("%d", &T);
while(T--){
scanf("%s", str);
printf("Case #%d: %d\n",kase++,search(str));
}
return 0;
}

 采用向量AC的代码624ms(传值要用引用,否则多了不必要的复制):

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<string>
#include<map>
#include<fstream>
#include<ctime>
#include<queue>
#include<vector>
#include<numeric>
#include<string.h>
#include<iomanip>
#include<sstream>
#include<algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef vector<int> BigInterger;
const int maxn = 100000;
const int maxsize = 10;
const int maxnum =65; struct TrieNode{
TrieNode(int d = -1) :id(d){ memset(next, NULL, sizeof(next)); }
int id;
TrieNode*next[maxsize];
};
TrieNode*T = new TrieNode; void insert(const BigInterger&v,const int&index){
int len=v.size();
TrieNode*p = T;
int t = 0;
for (int i = len-1; i>=0&&t<40;i--,t++){
int id =v[i];
if (!p->next[id])p->next[id] = new TrieNode(index);
p = p->next[id];
}
} int search(char*str){
TrieNode*p = T;
while (*str != '\0'){
int id = *str++ - '0';
if (!p->next[id])return -1;
p = p->next[id];
}
return p->id;
}
//引用传值,避免赋值,效率更高
void add(BigInterger &a, BigInterger &b, BigInterger&c){
int lena = a.size(), lenb = b.size();
int g=0, x,i=0;
c.clear();
if (lena > maxnum - 5 || lenb > maxnum - 5){
a.erase(a.begin()), b.erase(b.begin());
lena--, lenb--;
}
while (g || i < lena || i < lenb){
x = g;
if (i < lena)x += a[i];
if (i < lenb)x += b[i];
c.push_back(x % 10);
g = x / 10;
i++;
}
}
void init(){
BigInterger a[3];
a[0].push_back(1);
a[1].push_back(1);
insert(a[1], 0);
for (int i = 2; i<maxn; i++){
add(a[(i - 1) % 3], a[(i - 2) % 3], a[i % 3]);
insert(a[i % 3], i);
}
}
int main(){
init();
int T,kase=1;
char str[50];
scanf("%d", &T);
while(T--){
scanf("%s", str);
printf("Case #%d: %d\n",kase++,search(str));
}
return 0;
}

  

 

hdu4099 Revenge of Fibonacci的更多相关文章

  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 字典树

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

  3. hdu 4099 Revenge of Fibonacci 大数+压位+trie

    最近手感有点差,所以做点水题来锻炼一下信心. 下周的南京区域赛估计就是我的退役赛了,bless all. Revenge of Fibonacci Time Limit: 10000/5000 MS ...

  4. hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法

    Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...

  5. [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci

    题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...

  6. HDU 4099 Revenge of Fibonacci Trie+高精度

    Revenge of Fibonacci Problem Description The well-known Fibonacci sequence is defined as following: ...

  7. UVA 12333 Revenge of Fibonacci

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. HDU 4099 Revenge of Fibonacci(高精度+字典树)

    题意:对给定前缀(长度不超过40),找到一个最小的n,使得Fibonacci(n)前缀与给定前缀相同,如果在[0,99999]内找不到解,输出-1. 思路:用高精度加法计算斐波那契数列,因为给定前缀长 ...

  9. HDU 4099 Revenge of Fibonacci (数学+字典数)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4099 这个题目就是一个坑或. 题意:给你不超过40的一串数字,问你这串数字是Fibonacci多少的开头 ...

随机推荐

  1. 使用Cookie保存用户和密码然后自动登录

    login.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  2. 201621123034 《Java程序设计》第11周学习总结

    作业11-多线程 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThread ...

  3. codeforces ice cave

    /// /// 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 /// DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. ...

  4. BZOJ1233 [Usaco2009Open]干草堆tower 【单调队列优化dp】

    题目链接 BZOJ1233 题解 有一个贪心策略:同样的干草集合,底长小的一定不比底长大的矮 设\(f[i]\)表示\(i...N\)形成的干草堆的最小底长,同时用\(g[i]\)记录此时的高度 那么 ...

  5. Linux系统——提高编译速度的方法

    编译优化: 基本原则就是“以空间换时间” tmpfs: 解决IO瓶颈,充分利用本机内存资源 make -j: 充分利用本机计算资源 distcc: 利用多台计算机资源 ccache: 减少重复编译相同 ...

  6. typedef函数用法

    转载自:http://www.cnblogs.com/ggjucheng/archive/2011/12/27/2303238.html 引言 typedef 声明,简称 typedef,为现有类型创 ...

  7. Myeclipse中生成subscription code的代码

    //代码如下: package com.qls.AddingMethodsToAnEnum; import java.io.*; public class MyEclipseGen { private ...

  8. mysql 导入

    1.默认情况下:MySQL导入文件大小有限制的,最大为2M,所以当文件很大时候,直接无法导入,可修改php.ini参数调整: 在php.ini中修改相关参数: 影响MySQL导入文件大小的参数有三个: ...

  9. log4net配置,正在用

    <?xml version="1.0" encoding="utf-8" ?> <log4net> <appender name= ...

  10. UVA 10912 Simple Minded Hashing

    题意就略了.刚一看被数据吓住了.看到字符要求严格递增.那么如果字串长大于26那必然方案数目为0:同时1+2+3....+24+25+26=351如果大于这个数也是不可能的 令dp[i][j][k]表示 ...