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

Problem Description
The well-known Fibonacci sequence is defined as following:

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.

 
Input
  There are multiple test cases. The first
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.
 
Output
  For each test case, output the smallest
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.
 
Sample Input
15
1
12
123
1234
12345
9
98
987
9876
98765
89
32
51075176167176176176
347746739
5610
Sample Output
Case #1: 0
Case
#2: 25
Case #3: 226
Case #4: 1628
Case #5: 49516
Case #6: 15
Case #7:
15
Case #8: 15
Case #9: 43764
Case #10: 49750
Case #11: 10
Case #12: 51
Case #13: -1
Case #14: 1233
Case #15: 22374
Source
这题就是求以某串数字开头的斐波那契数列。
因为只要前40位数字。所以在加的时候长度大于50左右就截掉个位上的,保留高位。即从最高位开始插入进字典树,注意超过100000输出-1,也包括100000本事,即只需处理到9999项即可。
#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的更多相关文章

  1. UVA - 12333 Revenge of Fibonacci 高精度加法 + 字典树

    题目:给定一个长度为40的数字,问其是否在前100000项fibonacci数的前缀 因为是前缀,容易想到字典树,同时因为数字的长度只有40,所以我们只要把fib数的前40位加入字典树即可.这里主要讨 ...

  2. UVa 12333 - Revenge of Fibonacci manweifc(模拟加法竖式 & 字典树)

    题意: 给定n个(n<=40)数字, 求100000个以内有没有前面n个数字符合给定的数字的fibonacci项, 如果有, 给出最小的fibonacci项, 如果没有, 输出-1. 分析: 可 ...

  3. 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 ...

  4. UVa 12333 Revenge of Fibonacci (字典树+大数)

    题意:给定一个长度小于40的序列,问你那是Fib数列的哪一项的前缀. 析:首先用大数把Fib数列的前100000-1项算出来,注意,一定不能是100000,要不然会WA的,然后每个数取前40位,不足4 ...

  5. hdu 4099 Revenge of Fibonacci 大数+压位+trie

    最近手感有点差,所以做点水题来锻炼一下信心. 下周的南京区域赛估计就是我的退役赛了,bless all. Revenge of Fibonacci Time Limit: 10000/5000 MS ...

  6. hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法

    Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...

  7. HDU4099 Revenge of Fibonacci(高精度+Trie)

    Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 204800/204800 K (Java/ ...

  8. [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci

    题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...

  9. HDU 4099 Revenge of Fibonacci Trie+高精度

    Revenge of Fibonacci Problem Description The well-known Fibonacci sequence is defined as following: ...

随机推荐

  1. HDU 4462 Scaring the Birds (暴力枚举DFS)

    题目链接:pid=4462">传送门 题意:一个n*n的区域,有m个位置是能够放稻草人的.其余都是玉米.对于每一个位置(x,y)所放稻草人都有个作用范围ri, 即abs(x-i)+ab ...

  2. Install Qt 5 on Ubuntu(使用qt-opensource-linux-x64-5.7.0.run进行安装,而且是官方的wiki)

    Introduction This is a tutorial for installation of Qt 5.7.0 to Ubuntu 12.10. It may be used also fo ...

  3. HTML <button> 标签

    HTML <button> 标签 目标 实现点击button跳转到一个新的界面 参考文档 实例 以下代码标记一个按钮: <button type="button" ...

  4. hbase启动报错:Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0

    输入HBASE_MASTER_OPTS只是为了快速寻找这个选项而已,如果你手工找也可以 刚才那个命令回车后直接跳到这 前面加#就好了 修改后保存.重新启动hbase就好了. 注意:各个节点都要修改哦. ...

  5. Android自定义控件简单实现ratingbar效果

    先上图: 一开始让我自定义控件我是拒绝的,因为android很早以前就有一个控件ratingbar,但是设置样式的时候我发现把图片设置小一点就显示不全,一直找不到解办法!(可以设置系统的自带的小样式) ...

  6. 消息推送学习一、原生Socket的使用

    消息推送也是客户端和服务器连接然后进行交互的一种形式,但是不同于HTTP的连接,这种连接需要长时间的进行,当有消息时可以及时推送到客户端.除此之外还有多个用户,可能需要针对其身份进行不同的推送等等要求 ...

  7. Android 设置屏幕不待机

    本文转载于:http://blog.csdn.net/yudajun/article/details/7748760 androidnullservice 目录(?)[+] 最近做项目时正好用到,进行 ...

  8. 威联通 移动硬盘路径 /share/USBDisk1

    威联通 移动硬盘路径 /share/USBDisk1 cd /share/USBDisk3/Movies

  9. codeforces111D. Petya and Coloring(组合数学,计数问题)

    传送门: 解题思路: 要求一条直线分割矩阵时左右颜色数一样,那么就说明一个问题.直线左右移动时是不会改变左右矩阵的颜色集合的.所以说明:2~m-1列的颜色集一定属于第一列与第m列颜色集的交集.而且第一 ...

  10. Linux中为XEN网桥绑定物理网卡

    XEN虚拟机会默认将可以连通外网的网卡绑定到xenbr0上, 因此如果需要切换到其他物理网卡上时,需要自己配置脚本或执行命令. 1.添加脚本绑定 a.编写一个脚本,指定网卡与网桥绑定的关系 # vim ...