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: ...
随机推荐
- Java Exception和Error的差别
Java中异常的抽象类是Throwable,在此基础上.派生出两大类:Error和Exception. Error是程序中的严重错误,不应该用try-catch包括.Javadoc的说明例如以下: A ...
- JAVA爬虫Nutch、WebCollector的正则约束
爬虫爬取时,须要约束爬取的范围. 基本全部的爬虫都是通过正則表達式来完毕这个约束. 最简单的,正则: http://www.xinhuanet.com/.* 代表"http://www.xi ...
- Exchange2003迁移2010DAG的权限问题
exchange2010无法删除用户.在2010的控制台中新建一个通讯组.然后将它删除就会报告下面错误. MicrosoftExchange 错误:无法对对象"test"执行&qu ...
- zzuli--1812--sort(模拟水题)
1812: sort Time Limit: 1 Sec Memory Limit: 128 MB Submit: 158 Solved: 30 SubmitStatusWeb Board Des ...
- Linux常用视频播放器
1.SMplayer是一款跨平台的视频播放工具,可以支持大部分的视频和音频文件.它支持音频轨道切换.允许调节亮度.对比度.色调.饱和度.伽玛值,按照倍速.4倍速等多种速度回放.还可以进行音频和字幕延迟 ...
- 利用Python网络爬虫抓取微信好友的签名及其可视化展示
前几天给大家分享了如何利用Python词云和wordart可视化工具对朋友圈数据进行可视化,利用Python网络爬虫抓取微信好友数量以及微信好友的男女比例,以及利用Python网络爬虫抓取微信好友的所 ...
- Flask--Python中常用的Web框架之一
Web框架 什么是框架? 协助开发者快速开发web应程序的一套功能代码 开发者只需要按照框架约定要求,在指定位置写上自己的业务逻辑代码即可 为什么要用web框架? 使用web框架的主要目的就是避免重复 ...
- java web应用调用python深度学习训练的模型
之前参见了中国软件杯大赛,在大赛中用到了深度学习的相关算法,也训练了一些简单的模型.项目线上平台是用java编写的web应用程序,而深度学习使用的是python语言,这就涉及到了在java代码中调用p ...
- Top 16 Java 应用类 - 这些功能再也不用自己写了
Java中有很多应用类.这些类定义静态方法能够解决非常多常见的问题.以下是通过5万个开源项目统计得到的最热门的16个应用类. 类按热门程序排列.类的方法也是按热门程序排序. 浏览这个类能够看看有哪些功 ...
- Python Web框架Tornado的异步处理代码演示样例
1. What is Tornado Tornado是一个轻量级但高性能的Python web框架,与还有一个流行的Python web框架Django相比.tornado不提供操作数据库的ORM接口 ...