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: ...
随机推荐
- 关于post请求“CAUTION: Provisional headers are shown”【转】
在POST请求中偶尔会出现"CAUTION: Provisional headers are shown" 这个警告的意思是说:请求的资源可能会被(扩展/或其它什么机制)屏蔽掉. ...
- 基于matlab的音频波形实时採集显示 v0.1
robj = audiorecorder(44100,16,1); %设置採样频率.採样位数.通道数 recordblocking(robj,1); %採集初步数据(1s长度) rdata = get ...
- Java中二进制字节与十六进制互转
在Java中字节与十六进制的相互转换主要思想有两点: 1.二进制字节转十六进制时,将字节高位与0xF0做"&"操作,然后再左移4位,得到字节高位的十六进制A;将字节低位与0 ...
- mysql简单优化思路
mysql简单优化思路 作为开发人员,数据库知识掌握的可能不是很深入,但是一些基本的技能还是要有时间学习一下的.作为一个数据库菜鸟,厚着脸皮来总结一下 mysql 的基本的不能再基本的优化方法. 为了 ...
- 用三层交换建立企业VLAN
650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic.php?refimg=" ...
- XFCE 桌面环境美化,fedora27系统
一.添加RPM Fusion源,安装方法这里就不说了以前的文章里写过. 二.安装XFCE 主题管理器 xfce-theme-manager [root@Fedora ~]# dnf install x ...
- python数据处理技巧二
python数据处理技巧二(掌控时间) 首先简单说下关于时间的介绍其中重点是时间戳的处理,时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00 ...
- nginx下修改svn配置
最近公司的SVN服务器地址做了变更,而我用的操作系统是Ubuntu操作系统,我也不想把以前下载的代码重新进行修改,我想通过修改svn地址,应该可以,终于在网上通过查找资料,找到了解决的方法: ...
- android+myeclipse+mysql下拉框数据绑定
原创作品,允许转载,转载时请务必声明作者信息和本声明.http://www.cnblogs.com/zhu520/p/8027036.html 本人小白,那个大神看到有问题可指出,谢谢.... 一:我 ...
- Windows Server 2016 主域控制器搭建
基本上微软产品都需要依附于域控制器做身份认证,接下来我们一起来对Windows Server 2016 进行AD活动目录功能添加.1.更改服务器IP地址2.修改计算机名称(重新启动计算机)3.打开服务 ...