题目:

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

思路:

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

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

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. Delphi中WebBrowser控件打开部分网站报"Invalid floating point operation”解决

    Delphi中WebBrowser控件打开部分网站报"Invalid floating point operation”解决 EmbeddedWBWebBrowserDelphi  最近用E ...

  2. 加密散列算法——SHA-1

    与MD5算法类似,SHA-1算法也是在接收到消息后产生消息摘要,能够用来验证数据的完整性. 不同的是SHA1会产生一个160位的消息摘要(MD5产生的是128位). 其它的性质之类的不多说(请參考前面 ...

  3. Email-ext plugin

    https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin General This plugin extends Jenkins built i ...

  4. 【Codevs 2630】宝库通道

    http://codevs.cn/problem/2630/ Solution 预处理f[i][j],代表第j列前i行的代价 枚举上下界,然后做最大子段和,g[i]代表选到第i列的代价, g[k]=( ...

  5. expandableListview的默认箭头箭头怎样移到右边

    1 . ExpandableListView布局:<ExpandableListView    android:id="@+id/bbs_category_expandable_lis ...

  6. pip 清华镜像

    临时使用: 可以在使用pip的时候加参数-i  https://pypi.tuna.tsinghua.edu.cn/simple 例:pip install -i https://pypi.tuna. ...

  7. jQuery EasyUI,LinkButton(按钮)组件

    转自:https://www.cnblogs.com/adc8868/p/6639570.html jQuery EasyUI,LinkButton(按钮)组件 学习要点: 1.加载方式 2.属性列表 ...

  8. bzoj4534: 基础排序算法练习题

    传送门     策爷的论文题啊……题解在这儿 我只想知道为什么这题的弱化版会出现在我们今天的%你赛里…… 题意:给你一堆操作$(l,r)$,表示将区间$(l,r)$按升序排序.以及$q$个询问,每次询 ...

  9. C#+ItextSharp 查看pdf文件页面尺寸

    1# Nuget下载itextSharp,下载到本地 pm>Install-Package iTextSharp -Version 5.5.10 2# 引用dll,添加命名空间 using iT ...

  10. mac 修改用户权限

    想安装thinkPHP 下载完以后 访问报403错误 于是百度找 也没找到原因 自己猜测是不是用户权限问题 就是下面目录为tp的用户权限 不是root 其他是root的都能访问 于是百度搜了权限如何修 ...