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. volatile的含义

    从词面上来讲.volatile的意思是易变的,也就是说.在程序执行的过程中,有一些变量可能会被莫名其妙的改变,而优化器为了节约时间.有时候不会重读这个变量的真实值,而是去读在寄存器的备份,这种话,这个 ...

  2. JSunpack-n模拟WireShark拦截文件传输

    前言: 在前面的实验里我们进行了JSunpack-n的安装及其简单使用.JSunpack-n还有另外一些功能须要进行測试试验,由于本人也是刚接触这些东西.本文就当中一个"功能点"进 ...

  3. An internal error occurred during: &quot;Checking tomcat state&quot;. Error while reading server.xml

    An internal error occurred during: "Checking tomcat state". Error while reading server.xml ...

  4. hdu 3068 最长回文 【Manacher求最长回文子串,模板题】

    欢迎关注__Xiong的博客: http://blog.csdn.net/acmore_xiong?viewmode=list 最长回文                                 ...

  5. 洛谷P3807 【模板】卢卡斯定理exgcd

    题目背景 这是一道模板题. 题目描述 给定n,m,p(1\le n,m,p\le 10^51≤n,m,p≤105 ) 求 C_{n+m}^{m}\ mod\ pCn+mm​ mod p 保证P为pri ...

  6. 如何建立远程桌面连接(XP、Vista、Win7)

    如何建立远程桌面连接(XP.Vista.Win7) 要求: 1:对方即你要连的机器必须要允许远程桌面连接,操作系统一般是winXP(单用户)和win2003server(多用户),具体设置:右击我的电 ...

  7. 使用acme.sh快速生成SSL证书

    起因 早上收到了一封来自MySSL EE <noreply@notify.myssl.com>的邮件提示证书即将过期, 少于7天,但是acme.sh应该是60天自动renew的.于是查看下 ...

  8. PatentTips - System and method to deprivilege components of a virtual machine monitor

    BACKGROUND INFORMATION An embodiment of the present invention relates generally to virtualization pl ...

  9. Vijos——T1053 Easy sssp

    https://vijos.org/p/1053 描述 输入数据给出一个有N(2 <= N <= 1,000)个节点,M(M <= 100,000)条边的带权有向图. 要求你写一个程 ...

  10. Codeforces 429D Tricky Function 近期点对

    题目链接:点击打开链接 暴力出奇迹. 正解应该是近期点对.以i点为x轴,sum[i](前缀和)为y轴,求随意两点间的距离. 先来个科学的暴力代码: #include<stdio.h> #i ...