hdu4099 Revenge of Fibonacci
题意:给定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的更多相关文章
- HDU4099 Revenge of Fibonacci(高精度+Trie)
Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/ ...
- hdu4099 Revenge of Fibonacci 字典树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4099 思想很容易想到 就是预处理出前10w个的fib数,然后建树查询 建树时只用前40位即可,所以在计 ...
- hdu 4099 Revenge of Fibonacci 大数+压位+trie
最近手感有点差,所以做点水题来锻炼一下信心. 下周的南京区域赛估计就是我的退役赛了,bless all. Revenge of Fibonacci Time Limit: 10000/5000 MS ...
- hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法
Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...
- [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci
题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...
- HDU 4099 Revenge of Fibonacci Trie+高精度
Revenge of Fibonacci Problem Description The well-known Fibonacci sequence is defined as following: ...
- UVA 12333 Revenge of Fibonacci
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- HDU 4099 Revenge of Fibonacci(高精度+字典树)
题意:对给定前缀(长度不超过40),找到一个最小的n,使得Fibonacci(n)前缀与给定前缀相同,如果在[0,99999]内找不到解,输出-1. 思路:用高精度加法计算斐波那契数列,因为给定前缀长 ...
- HDU 4099 Revenge of Fibonacci (数学+字典数)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4099 这个题目就是一个坑或. 题意:给你不超过40的一串数字,问你这串数字是Fibonacci多少的开头 ...
随机推荐
- Linux学习15_CentOS6.5下netcat工具安装教程
1.下载 下载地址:http://sourceforge.net/projects/netcat/files/netcat/0.7.1/ 下载的是netcat-0.7.1.tar.gz版本 2.拷贝 ...
- 再理一下prerouting和postrouting等插入点
这些地方的准确翻译是hook点(hook点是一个土的说法,学名叫rule chain,规则链)这些规则链是内核netfilter架构布置在内核里面的,然后iptables是利用了这套基础架构,想起了内 ...
- lambda表达式10个示例——学习笔记
摘录:http://www.importnew.com/16436.html 1.lambda实现Runnable // Java 8之前: new Thread(new Runnable() { @ ...
- nodejs & docker
nodejs & docker https://github.com/xgqfrms-GitHub/Node.js/blob/master/Docker-Nodejs/translation. ...
- [TJOI2017][bzoj4889] 不勤劳的图书管理员 [线段树套线段树]
题面 传送门 思路 考虑两本书的位置交换对答案的贡献: (为了方便描述,用"左边那本"和"右边那本"称呼两本我们要交换的书,"中间那本"是我 ...
- 白白的(baibaide)
白白的(baibaide) 有一个长度为 $n$ 的序列 $a_1, a_2, \dots, a_n$,一开始每个位置都是白色.如果一个区间中每个位置都是白色,则称这是一个白白的区间.如果一个白白的区 ...
- Codeforces Round #522 (Div. 2) C. Playing Piano
C. Playing Piano 题目链接:https://codeforces.com/contest/1079/problem/C 题意: 给出数列{an},现在要求你给出一个数列{bn},满足: ...
- vs2008升级正式版
1.VS2008简体中文正式版序列号 1.Visual Studio 2008 Professional Edition: XMQ2Y-4T3V6-XJ48Y-D3K2V-6C4WT 2.Visual ...
- js对象和jq对象互相转换
1.DOM 对象转成 jQuery 对象 var v = document.getElementById("v"); //DOM对象 var $v = $(v); //jQuery ...
- python 独立环境安装
python 即使是单独编译安装的,库文件的安装还是会与其它python的库存放到相同的地方 使用同版本库不会有问题,但是需要升级库的时候,就会出现冲突,导致依赖这个旧库的python出现问题 这时候 ...