Revenge of Fibonacci

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
 
题解:前100000个斐波那契数,太大所以用高精度预处理出前缀,我们只存50位就可以,出现51位,我们就删除个位,保留高位,插入trie树中
///
#include<bits/stdc++.h>
using namespace std; typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//****************************************
const int N=+;
#define maxn 100000+5 struct Trie{
int ch[N*][],sum[N*],siz;
void init() {mem(ch),mem(sum),siz=;}
void insertt(int c[],int index) {
int u=,len=c[];int cc=;
for(int i=;i<=min(len,);i++) {
int v=c[i];
if(ch[u][v]==) {
sum[siz] = index;
ch[u][v] = siz++;
}
u=ch[u][v];
}
}
int aks(int c[]) {
int u=;
for(int i=;i<=c[];i++) {
if(ch[u][c[i]]==) return -;
u=ch[u][c[i]];
}
return sum[u];
}
}trie;
int a[],b[],c[],d[];
int main() {
mem(a),mem(b),mem(c);
trie.init();
a[]=a[]=;
trie.insertt(a,);
b[]=b[]=;
trie.insertt(b,);
for(int i=;i<;i++) {
int len=b[];
if(len>) {
for(int j=;j<a[];j++) a[j]=a[j+];a[a[]]=;a[]--;
for(int j=;j<b[];j++) b[j]=b[j+];b[b[]]=;b[]--;
len--;
}
//for(int j=0;j<100;j++)c[j]=0;
len=max(a[],b[]);
for(int j=;j<=len;j++) c[j]=a[j]+b[j];
for(int j=;j<len;j++) if(c[j]>) c[j+]++,c[j]=c[j]%;
if(c[len]>) c[len]%=,c[len+]=,len++;
c[]=len;
int h=;
for(int j=c[];j>=;j--) d[++h]=c[j];
d[]=c[];
trie.insertt(d,i); for(int j=;j<=b[];j++) a[j]=b[j];
for(int j=;j<=c[];j++) b[j]=c[j];
}
int T=read();
int oo=;
while(T--) {
char s[];
scanf("%s",s);
int tmp[];
for(int i=;i<strlen(s);i++) tmp[i+]=s[i]-'';
tmp[]=strlen(s);
printf("Case #%d: %d\n",oo++,trie.aks(tmp));
}
return ;
}

代码

HDU 4099 Revenge of Fibonacci Trie+高精度的更多相关文章

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

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

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

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

  3. HDU 4099 Revenge of Fibonacci(高精度+字典树)

    题意:对给定前缀(长度不超过40),找到一个最小的n,使得Fibonacci(n)前缀与给定前缀相同,如果在[0,99999]内找不到解,输出-1. 思路:用高精度加法计算斐波那契数列,因为给定前缀长 ...

  4. HDU 4099 Revenge of Fibonacci (数学+字典数)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4099 这个题目就是一个坑或. 题意:给你不超过40的一串数字,问你这串数字是Fibonacci多少的开头 ...

  5. hdu 4099 Revenge of Fibonacci 字典树+大数

    将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...

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

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

  7. HDU 1250 Hat's Fibonacci(高精度)

    Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...

  8. hdu 5018 Revenge of Fibonacci

    大水题 #include<time.h> #include <cstdio> #include <iostream> #include<algorithm&g ...

  9. HDU 4099 大数+Trie

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

随机推荐

  1. C#中的分层开发

    一般来说,分层主要分三层即:UI(User Interface) 界面显示层,BLL(Business Logic Layer)业务逻辑层,以及DAL(Data Access Layer)数据访问层. ...

  2. 怎样用Fiddler模拟网络超时

    转自:http://materliu.github.io/all/web/2014/04/28/fiddler-timeout.html   用fiddler模拟网络请求超时 用fiddler模拟网络 ...

  3. java_io学习_编码

    package io; public class encodingDemo{ public static void main(String[] args) throws Exception{ // T ...

  4. PHP 之CURL请求封装GET、POST、PUT、DELETE

    /** * @Description: curl请求 * @Author: Yang * @param $url * @param null $data * @param string $method ...

  5. 关于static关键字的思考

    静态方法是否能调用非静态成员变量?    static关键字具有如下特点:        一.static关键字修饰的属性/方法可以通过类名直接调用,而不必先new一个对象.        二.sta ...

  6. UNIX C XSI_IPC对象、共享内存

    1.创建IPC对象 #include <sys/ipc.h> key_t ftok(const char* pathname,int proj_id); 成功返回可用于创建或获取IPC的键 ...

  7. HDU 4027(线段树)

    HDU4027 题意:操作指令为0时,对区间[x,y]之间的数字进行开平方:指令为1的时候,对区间[x,y]之间的数字求和并输出: 思路:线段树处理就OK了,但是64位内的数最多开8次平方就为1了(开 ...

  8. python爬虫15 | 害羞,用多线程秒爬那些万恶的妹纸们,纸巾呢?

    有时候 只是在人群中多看了一眼 就再也没办法忘掉那些容颜 小帅b在普通的一天 上着普通的网 不小心打开了一个不太普通的网站 https://www.mzitu.com/ 从此进入了不普通的一天 看着不 ...

  9. 搭建 Seafile 专属网盘

    准备域名 任务时间:15min ~ 20min 域名注册 如果您还没有域名,可以在腾讯云上选购,过程可以参考下面的视频. 视频 - 在腾讯云上购买域名 域名解析 域名购买完成后, 需要将域名解析到实验 ...

  10. STM32学习笔记:读写内部Flash(介绍+附代码)

    一.介绍 首先我们需要了解一个内存映射: stm32的flash地址起始于0x0800 0000,结束地址是0x0800 0000加上芯片实际的flash大小,不同的芯片flash大小不同. RAM起 ...