题目:

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

思路:

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

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

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. idea mac 控制台中文乱码

    参考:https://blog.csdn.net/lheangus/article/details/48915357 修改内容 -Dfile.encoding=UTF-8

  2. 【USACO 2017FEB】 Why Did the Cow Cross the Road III

    [题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 100010 ...

  3. pycharm打开多个项目并存

    问题: 有时我们需要打开多个项目,而现在的做法是:  原有的a项目不动,新打开一个pycharm来打开b项目, 或者 在原有的a项目中打开b项目并覆盖a项目,即a项目与b项目不能共存 需求: 有时我们 ...

  4. NOIP 2011 Mayan游戏 大暴搜

    题目链接:https://www.luogu.org/problemnew/show/P1312 我的第一篇题解!! 当然感谢ZAGER 的提示,他的链接https://www.cnblogs.com ...

  5. c#.net常用函数列表

    .DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒 currentTime=System.Da ...

  6. hdu 2209 翻纸牌游戏【贪心】

    本来是冲着搜索去的--其实可以贪心 因为能改变第一位的只有第一位和第二位,然后改完之后后面的同理,也就是说只要贪心的推一遍就可以 但是注意要在翻第一个和不翻第一个之间取个min #include< ...

  7. 牛客OI周赛2-提高组

    A.游戏 链接:https://www.nowcoder.com/acm/contest/210/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语 ...

  8. nginx的负载均衡的问题

    本节就聊聊采用Nginx负载均衡之后碰到的问题: Session问题 文件上传下载 通常解决服务器负载问题,都会通过多服务器分载来解决.常见的解决方案有: 网站入口通过分站链接负载(天空软件站,华军软 ...

  9. java大数轻松过

    import java.util.Scanner; import java.math.BigInteger; public class Main { public static void main(S ...

  10. ACM_01背包

    背包1 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个重量和价值分别为Wi,Vi的物品,现从这些物品中挑选出总量不超过 W ...