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. Json解析与Gson解析

    本文主要介绍json最原始的解析与google提供的gson工具类解析 ①json解析 /** * 普通的json解析 * @param s * @throws JSONException */ pr ...

  2. iOS 从xib中加载自定义视图

    想当初在学校主攻的是.NET,来到公司后,立马变成java开发,之后又跳到iOS开发,IT人这样真的好么~~  天有不测风云,云还有变幻莫测哎,废话Over,let's go~ 新学iOS开发不久,一 ...

  3. Android集成二维码扫描功能

    文章转载自  https://github.com/yipianfengye/android-zxingLibrary 在具体介绍该扫描库之前我们先看一下其具体的使用方式,看看是不是几行代码就可以集成 ...

  4. 扩增子图表解读1箱线图:Alpha多样性

    箱线图 箱形图(Box-plot)又称为盒须图.盒式图或箱线图,是一种用作显示一组数据分散情况资料的统计图.因形状如箱子而得名.在宏基因组领域,常用于展示样品组中各样品Alpha多样性的分布 第一种情 ...

  5. cocos creator游戏适配这事

    在想cocos适配之前,我们想想网页是怎么适配的.浏览器有各种规格,网页的一般做法是:背景图片铺满,网页内容保持在背景图片上居中,就实现了适应或者适配.css一般这样: .bg{ height:582 ...

  6. 解决fixed在苹果手机抖动问题/头部底部固定布局

    1.头部和底部固定,中间内容滚动,不涉及fixed 1)absolute与height:100% <!DOCTYPE html><html><head> <m ...

  7. windows设置右键菜单

    1. 打开注册表,(win + R,输入regedit) 2. 在 HKEY_CLASSES_ROOT\Directory\Background\shell 中,新建项:如(cmder),在 cmde ...

  8. 【转载】Linux下各文件夹的含义和用途

    原文地址:https://www.cnblogs.com/lanqingzhou/p/8037269.html Linux下各文件夹的含义和用途 Linux根目录”/“下各个系统文件夹的含义和用途 1 ...

  9. react 子组件给父组件传值

    import React from 'react'import '../page1/header.css'import { Table } from 'antd'import Child from ' ...

  10. 初学者对ASCII编码、Unicode编码、UTF-8编码的理解

    最早的计算机在设计时采用8个比特(bit)作为一个字节(byte),所以,一个字节能表示的最大的整数就是 255(二进制 11111111=十进制 255),如果要表示更大的整数,就必须用更多的字节. ...