题意:给定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. LOJ #124. 除数函数求和 1

    题目描述 $\sigma_k(n) = \sum_{d | n} d ^ k$​ 求 $\sum_{i=1}^n\sigma_k(i)$ 的值对 109 取模的结果. 输入格式 第一行两个正整数 n, ...

  2. [bzoj3218] a+b problem [最小割+数据结构优化建图]

    题面 传送门 思路 最小割 我们首先忽略掉那个奇♂怪的限制,就有一个比较显然的最小割模型: 建立源点$S$和汇点$T$ 对于每个元素$i$建立一个点$i$,连边$<S,i,w[i]>$和$ ...

  3. BZOJ1025 [SCOI2009]游戏 【置换群 + 背包dp】

    题目链接 BZOJ1025 题解 题意就是问一个\(1....n\)的排列在同一个置换不断重复下回到\(1...n\)可能需要的次数的个数 和置换群也没太大关系 我们只需知道同一个置换不断重复,实际上 ...

  4. 为IE和chrome编写单独的样式

    flex布局在IE10上不支持,为IE10编写特定的样式可以用判断navigator.userAgent 的方法 var doc = document.documentElement; doc.set ...

  5. HDU1536&&POJ2960 S-Nim(SG函数博弈)

    S-Nim Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status ...

  6. [ CodeVS冲杯之路 ] P1098

     不充钱,你怎么AC? 题目:http://codevs.cn/problem/1098/ 显然就是使每堆牌达到总体的平均数,尽量使每次移动时的牌数最大,这就类似于飞行棋,将几个棋子叠起来一起走是最优 ...

  7. [字符串] StartWith和EndWith效率比较低

    之前无意中看到有人提到StartWith和EndWith效率比较低,今天恰好有用到这样的场景,于是写了个测试验证一下. 该示例仅在比较字符串首尾单个字符,用途有限. var str = "\ ...

  8. apache快速配置简易网站

    网站源文件目录 F:\01.prj\site\static_html F:\01.prj\site\static_html 打开apache_http.conf 1 .修改网站目录 错误1 Alias ...

  9. kvm虚拟机最佳实践系列3-kvm克隆和静态迁移

    KVM克隆和KVM静态迁移 KVM克隆 上一章我们已经有了一个合用的虚拟机镜像,现在我们需要用这个KVM镜像大量的创建和部署 virt-clone就是做这个用的.它简化了我们克隆KVM的步骤. 首先停 ...

  10. SqlServer高版本数据库数据备份到低版本数据库上

    想要将Sqlserver2014高版本备份的数据还原到低版本SqlServer2012上去,但是这在SqlServer中是没法直接还原数据库的,通过以下方法可以顺利还原. 通过高版本生成sql脚本在低 ...