题目:

给出一个斐波那契数字的前缀,问第一个有这个前缀的数字在斐波那契数列中是第几个。

思路:

紫书提示:本题有一定效率要求。如果高精度代码比较慢,可能会超时。

利用滚动数组和竖式加法来模拟斐波那契相加的过程,在这个过程中每得出一个斐波那契数字就用字典树存一下。

PS:在滚动数组中存的斐波那契数字是逆序存储的。

代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define MAX 1e9;
#define FRE() freopen("in.txt","r",stdin)
#define FRO() freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
const int maxn = ;
struct Node {
int next[];
int id;
Node() {
for(int i = ; i<; i++) {
next[i] = -;
}
id = -;
}
};
Node trie[maxn];
int tot = ;
//Node root = trie[tot++]; int idx(char ch) {return ch-'';} void Insert(char* str, int id) {//字典树插入
// int u = 0;
// for(int i = 0; i<strlen(str); i++) {
// int v = idx(str[i]);//获取字符串当前位置的数字
// if(trie[u].next[v]==-1) { //如果这个位置是不存在的
// trie[u].next[v] = tot;
// u = tot++;
// } else { //如果这个位置是存在的
// u = trie[u].next[v];
// }
// if(trie[u].id==-1) {
// trie[u].id = id;
// }
// }
int u = ;
for(int i = ; i<strlen(str); i++){
int v = idx(str[i]);
if(trie[u].next[v]==-){//如果这个位置不存在
trie[u].next[v] = tot++;
}
u = trie[u].next[v];//结点往下走
if(trie[u].id==-){//保存这个斐波那契数字的id
trie[u].id = id;
}
}
} int Query(char* str) {
int u = ;
for(int i = ; i<strlen(str); i++) {
int v = idx(str[i]);
if(trie[u].next[v]==-) { //说明这个前缀不会出现
return -;
}
u = trie[u].next[v];
}
return trie[u].id;
} int fib[][maxn];
int main() {
FRE();
int s=,l=;
char f[];
Insert("",);
fib[][] = ;
fib[][] = ;
for(int i=; i<; i++) {
int a=i%, b=(i+)%;//滚动数组,可以手动跑两组斐波那契数字的相加就可以理解了
for(int k=s; k<l; k++) {
fib[a][k] = fib[a][k] + fib[b][k];
if(fib[a][k]>=) { //这位相加可以进位时
fib[a][k+]++;//下一位加一
fib[a][k] -= ;//这一位减掉进位的10
if(k==l-) { //最后一个进位让l加一
l++;
}
}
}
if(l-s>) { //数组中是倒着存这个数的,l-1表示个位开始,s表示最高位
s++;
}
int cnt=;
for(int k=l-; k>=s&&cnt<; k--){//根据题目提示,只选取前40位即可
f[cnt++] = fib[a][k]+'';
}
// if(i==10)
// printf("%s\n",f);
Insert(f,i);
memset(f,,sizeof(f));
}
//cout<<"GG"<<endl;
char str[];
int n;
scanf("%d",&n);
for(int i=; i<n; i++){
scanf("%s",str);
printf("Case #%d: ",i+);
int ans = Query(str);
printf("%d\n",ans==- ? -:ans-);
}
return ;
}

UVA-12333 Revenge of Fibonacci(竖式加法模拟 & 字典树)的更多相关文章

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

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

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

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

  3. UVA 12333 Revenge of Fibonacci

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

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

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

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

  6. uva 10274 Fans and Gems(隐式图搜索+模拟)

    Fans and Gems Input: Standard Input Output: Standard Output Tomy's fond of a game called 'Fans and G ...

  7. N分之一 竖式除法模拟

    N分之一 Description Alice越来越痴迷于数学问题了.一天,爸爸出了个数学题想难倒她,让她求1 / n. 可怜的Alice只有一岁零九个月,回答不上来 ~~~~(>_<)~~ ...

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

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

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

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

随机推荐

  1. 基于Spark和Tensorflow构建DCN模型进行CTR预测

    实验介绍 数据采用Criteo Display Ads.这个数据一共11G,有13个integer features,26个categorical features. Spark 由于数据比较大,且只 ...

  2. 在数据库中生成txt文件到网络驱动器中(计算机直接创建的网络驱动器在sql server中没有被找到)

    环境:sql server 2008 一.创建网络驱动器映射 语法:exec master..xp_cmdshell 'net use Z: \\ip地址\网络路径 密码 /user:用户名' 例如: ...

  3. bzoj2093: [Poi2010]Frog(单调队列,倍增)

    2093: [Poi2010]Frog Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 568  Solved: 186[Submit][Status] ...

  4. akka监控框架设计

    本博客介绍一种AOP.无侵入的akka监控方案,方便大家在生产使用akka的过程中对akka进行监控. 对于自身javaer来说,AOP三个字母基本就解释清楚了akka监控框架的原理.哈哈哈,不过我这 ...

  5. WMI 技术

    什么是WMI? Windows Management Instrumentation (WMI)是可伸缩的系统管理结构,该规范采用一个统一.基于标准且可扩展的面向对象接口.它提供与系统管理员信息和基础 ...

  6. [转]深入C语言内存区域分配(进程的各个段)详解

    一般情况下,一个可执行二进制程序(更确切的说,在Linux操作系统下为一个进程单元,在UC/OSII中被称为任务)在存储(没有调入到内存运行)时拥有3个部分,分别是代码段(text).数据段(data ...

  7. js实现水波纹背景

    <!DOCTYPE html> <html> <head> <title>水波背景</title> <meta charset=&qu ...

  8. bash、dash(/bin/bash和/bin/sh)的区别

    Linux中的shell有多种类型,其中最常用的几种是Bourne   shell(sh).C   shell(csh)和Korn   shell(ksh).三种shell各有优缺点. Bourne ...

  9. node-rsa加密,java解密调试

    用NODE RSA JS 加密解密正常,用JAVA RSAUtils工具类加密解密正常.但是用node加密玩的java解密不了.原因:node默认的是 DEFAULT_ENCRYPTION_SCHEM ...

  10. 简单的win7-cmd命令提示符

    在win7打开cmd窗口 有两个路径:(1)开始 -->所有程序 --> 附件 --> 命令提示 (2)开始 -->在搜索框输入 “cmd”   指令 作用 对文件夹的操作   ...