UVA 12333 Revenge of Fibonacci
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3755
Revenge of Fibonacci
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/Others)
Total Submission(s): 914 Accepted Submission(s): 197
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.
line of input contains a single integer T denoting the number of test
cases (T<=50000).
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.
index of the smallest Fibonacci number whose decimal notation begins
with the given digits. If no Fibonacci number with index smaller than
100000 satisfy that condition, output -1 instead – you think what
Fibonacci wants to told you beyonds your ability.
#2: 25
15
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int t,cast,f1[],f2[],f3[];
char a[];
struct trie
{
ll isend;
struct trie *next[];
trie()
{
isend=-;
for(int i=;i<=;i++)
next[i]=NULL;
}
};
trie *root=new trie();
void insert(trie *root,char *s,int k)
{
trie *p=root;
trie *tmp;
int len=strlen(s);
for(int i=;i<len;i++)
{
if(p->next[s[i]-'']==NULL)
{
tmp=new trie();
p->next[s[i]-'']=tmp;
}
p=p->next[s[i]-''];
if(p->isend<) p->isend=k;
}
}
int find(trie *root,char *s)
{
trie *p=root;
int k;
int len=strlen(s);
for(int i=;i<len;i++)
{
p=p->next[s[i]-''];
if(p==NULL)
return -;
else k=p->isend;
}
return k;
}
void init()
{
char b[]="";
memset(f1,,sizeof(f1));
memset(f2,,sizeof(f2));
memset(f3,,sizeof(f3));
f1[]=;f2[]=;
insert(root,b,);
for(int i=;i<;i++)
{
memset(b,,sizeof(b));
int cnt=,k;
for(int j=;j<;j++)
{
f3[j]=f1[j]+f2[j]+cnt;
cnt=f3[j]/;
f3[j]%=;
}
for(int j=;j>=;j--)
{
if(f3[j]){k=j;break;}
}
int pos=;
for(int j=k;j>=;j--)
{
b[pos++]=f3[j]+'';
if(pos>=) break;
}
insert(root,b,i);
if(k>)
{
for(int j=;j<;j++)//舍弃个位我i,保留高位
f3[j-]=f3[j];
for(int j=;j<;j++)
f2[j-]=f2[j];
}
for(int j=;j<;j++)
f1[j]=f2[j];
for(int j=;j<;j++)
f2[j]=f3[j];
}
}
int main()
{
init();
scanf("%d",&t);
cast=t;
while(t--)
{
scanf("%s",a);
printf("Case #%d: ",cast-t);
printf("%d\n",find(root,a));
}
return ;
}
UVA 12333 Revenge of Fibonacci的更多相关文章
- UVA - 12333 Revenge of Fibonacci 高精度加法 + 字典树
题目:给定一个长度为40的数字,问其是否在前100000项fibonacci数的前缀 因为是前缀,容易想到字典树,同时因为数字的长度只有40,所以我们只要把fib数的前40位加入字典树即可.这里主要讨 ...
- UVa 12333 - Revenge of Fibonacci manweifc(模拟加法竖式 & 字典树)
题意: 给定n个(n<=40)数字, 求100000个以内有没有前面n个数字符合给定的数字的fibonacci项, 如果有, 给出最小的fibonacci项, 如果没有, 输出-1. 分析: 可 ...
- UVA - 12333 Revenge of Fibonacci (大数 字典树)
The well-known Fibonacci sequence is defined as following: F(0) = F(1) = 1 F(n) = F(n − 1) + F(n − 2 ...
- 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 ...
- hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法
Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...
- HDU4099 Revenge of Fibonacci(高精度+Trie)
Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/ ...
- [刷题]算法竞赛入门经典(第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: ...
随机推荐
- 利用opencv源代码和vs编程序训练分类器haartraining.cpp
如需转载请注明本博网址:http://blog.csdn.net/ding977921830/article/details/47733363. 一 训练框架 训练人脸检測分类器须要三个步骤: (1 ...
- vue11 vue实例方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue5 过滤器 模版
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [ Linux ] 釋放記憶體指令(cache) - 轉載
1. [Linux]釋放記憶體指令(cache) http://jeffreyy.pixnet.net/blog/post/84333764-%E3%80%90linux%E3%80%91%E9%87 ...
- IIS 优化
http://www.cnblogs.com/wangjingblogs/archive/2013/02/27/2934706.html 通过对IIS7的配置进行优化,调整IIS7应用池的队列长度,请 ...
- uniq---报告或忽略文件中的重复行
uniq命令用于报告或忽略文件中的重复行,一般与sort命令结合使用. 语法 uniq(选项)(参数) 选项 -c或——count:在每列旁边显示该行重复出现的次数: -d或--repeated:仅显 ...
- 51nod 正整数分组
将一堆正整数分为2组,要求2组的和相差最小. 显然我们可以把所有可能组合成的数求出来. 然后从总和的中间开始往大找,找到了就是其中一个的分组,就可以求出答案了. #include<cstdio& ...
- wampserver-mysql创建数据库
首先打开wampserver,在右下角会出现一个这样的图标,左键单击它,选择MYSQL->MYSQL控制台 输入密码 创建一个新的数据库:create database XXX 注意要输“;”, ...
- Android学习总结(2)——App客户端与服务器交互中的token
学习Token Token是什么? Token是服务端生成的一串字符串,以作客户端进行请求的一个令牌,当第一次登录后,服务器生成一个Token便将此Token返回给客户端,以后客户端只需带上这个Tok ...
- JBoss配置连接池
什么是数据库连接池? 配置连接池为的是解决效率问题.由于每创建一个连接都是非常耗时的,有了连接池,就能够提前放一些连接进去.以后我们再用连接就去连接池里面取而不是每次都创建.可是我们知道连接池是有上限 ...