题意:给定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. DOM对象转化成jQuery对象

    相比较jQuery转化成DOM,开发中更多的情况是把一个DOM对象加工成jQuery对象.$(参数)是一个多功能的方法,通过传递不同的参数而产生不同的作用. 如果传递给$(DOM)函数的参数是一个DO ...

  2. 【转】PHP对象在内存中的分配

    对像在PHP 里面和整型.浮点型一样,也是一种数据类,都是存储不同类型数据用的, 在运行的时候都要加载到内存中去用,那么对象在内存里面是怎么体现的呢?内存从逻辑上 说大体上是分为4 段,栈空间段.堆空 ...

  3. jmeter的build.xml

    <?xml version="1.0"?><!--   Licensed to the Apache Software Foundation (ASF) unde ...

  4. BZOJ2716 [Violet 3]天使玩偶 【CDQ分治】

    题目 输入格式 输出格式 输入样例 //样例太长就不贴了.... 输出样例 //见原题 提示 题解 我们将曼哈顿距离式子中的绝对值去掉,每次只考虑x,y比当前点小的更新答案. 为了使所有点都对答案进行 ...

  5. python下载链接内容

    下面代码下载京东注册码,可接收参数num dir 可以将连接构造成其它网址,比如移动联通网上营业厅的验证码都是固定网址+13位时间戳的结构. #!/usr/bin/python #code utf-8 ...

  6. POJ2187 旋转卡壳 求最长直径

    给定平面上的一些散点集,求最远两点距离的平方值. 题解: 旋转卡壳求出凸包,然后根据单调性,求出最远两点的最大距离 #pragma GCC optimize(2) #pragma G++ optimi ...

  7. 解决hibernate向mysql插入中文乱码问题(更改MySQL字符集)

    1.首先需要修改mysql数据库的配置文件my.ini,此文件放在mysql根目录下.在此文件下查找default-character-set属性,并将其值更改为utf8(注意:不是utf-8,也要注 ...

  8. 总结各种width,height,top,left

    1.offsetWidth 内容+内边距(padding)+边框(border) 2.offsetHeight 内容+内边距(padding)+边框(border) 3.offsetLeft 获取对象 ...

  9. 日志组件Log4Net

    <?xml version="1.0" encoding="utf-8"?> <configuration> <configSec ...

  10. 小Z爱划水(NOIP信(sang)心(bin)赛)From FallDream

    题目: 小Z在机房.他和其它机房同学都面临一个艰难的抉择,那就是 要不要划水? 每个人都有自己的一个意见,有的人想做题,有的人想划水. 当然,每个人只能选择一个事情做.如果一个人做的事情和他想做的不同 ...