HDU 4099 Revenge of Fibonacci Trie+高精度
Revenge of Fibonacci
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.
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.
1
12
123
1234
12345
9
98
987
9876
98765
89
32
51075176167176176176
347746739
5610
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+高精度的更多相关文章
- hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法
Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...
- hdu 4099 Revenge of Fibonacci 大数+压位+trie
最近手感有点差,所以做点水题来锻炼一下信心. 下周的南京区域赛估计就是我的退役赛了,bless all. Revenge of Fibonacci Time Limit: 10000/5000 MS ...
- HDU 4099 Revenge of Fibonacci(高精度+字典树)
题意:对给定前缀(长度不超过40),找到一个最小的n,使得Fibonacci(n)前缀与给定前缀相同,如果在[0,99999]内找不到解,输出-1. 思路:用高精度加法计算斐波那契数列,因为给定前缀长 ...
- HDU 4099 Revenge of Fibonacci (数学+字典数)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4099 这个题目就是一个坑或. 题意:给你不超过40的一串数字,问你这串数字是Fibonacci多少的开头 ...
- hdu 4099 Revenge of Fibonacci 字典树+大数
将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...
- HDU4099 Revenge of Fibonacci(高精度+Trie)
Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/ ...
- HDU 1250 Hat's Fibonacci(高精度)
Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...
- hdu 5018 Revenge of Fibonacci
大水题 #include<time.h> #include <cstdio> #include <iostream> #include<algorithm&g ...
- HDU 4099 大数+Trie
Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/ ...
随机推荐
- SQL 循环插入10000条
SQL> create table tt_test ( x int, y char(50) ); Table created. SQL> SQL> begin 2 for i in ...
- JS——“==”与“===”
==: 两个等于号只是比较两个变量的值 var n1 = 1; var n2 = "1"; alert(n1 == n2);//返回true ===: 三个等于号不仅比较值而且比较 ...
- Prime算法生成最小生成树
虽说是生成树,但我只将生成的边输出了.至于怎么用这些边来创建树...我不知道_(:з」∠)_ //Prime方法生成最小生成树 void GraphAdjacencyListWeight::Gener ...
- 汇总——WEB前端资源网
前端攻城师 爱思资源网 HTML5吧 0101后花园 前端网 编程教程和源代码示例 Javascript中文网 Web前端资源网 移动端HTML5资源整理 Web开发者 SegmentFault 前端 ...
- HDU_1068_Girls and Boys_二分图匹配
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- eclipse常用设置之自动格式化
Eclipse 保存文件时自动格式化代码 很多同学不知道Eclipse有个很有用的功能,就是自动格式源代码的功能,一般大家都是直接Ctrl+Shift+F手动格式化,多浪费时间. 其实Eclips ...
- Python 函数 day3
函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可以自己创建函数,这 ...
- JavaScript day4(条件语句和条件运算符)
1. 布尔值 布尔值要么是 true 要么是 false .它非常像电路开关, true 是“开”,false 是“关”.这两种状态是互斥的. 2. if 语句 if 语句用于在代码中做条件判断.关键 ...
- Python变量的命名 单下划线和双下划线
python命名变量的区别 foo: 一种约定,Python内部的名字,用来区别其他用户自定义的命名,以防冲突,就是例如__init__(),__del__(),__call__()这些特殊方法 _f ...
- PostgreSQL使用总结
最近项目用到了PostgreSQL数据库,网上一堆教程,这里自己整理一下做个笔记: 1,下载安装,我这边安装在Windows7,在这里找到大象一样的标志: 2,双击打开,这里的话按流程直接走: 3,这 ...