高精度 trie

暴力预处理出前100000个fibonacci数,将每个数的前40位数字串插入到trie中,记录每个结点最早可以由哪个数字串到达。

然后依次回答询问即可。

存fibonacci数的数组当然要滚动起来。

时限是10秒。本机试着卡了常数后跑了18秒(这个高精度写法好像本来就很慢),干脆交一波,UVA上7s AC

        ↑for循环i+=4 和直接一个register跑出来一样快,果然WC2017那道题只能在特定机子上做吗233

        ↑本机CPU是Intel I5-4590

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<time.h>
using namespace std;
const int mxn=;
struct Num{
int x[];
int len;
friend Num operator + (Num a,Num b){
a.len=max(a.len,b.len);
// printf("len:%d\n",a.len);
int ed=a.len/*;
for(int i=;i<=ed;i+=){
a.x[i]+=b.x[i];
a.x[i+]+=b.x[i+];
a.x[i+]+=b.x[i+];
a.x[i+]+=b.x[i+];
}
for(int i=ed+;i<=a.len;i++)
a.x[i]+=b.x[i]; // for(register int i=1;i<=a.len;i++) a.x[i]+=b.x[i]; for(int i=;i<=a.len;i++){
if(a.x[i]>=){
a.x[i+]+=a.x[i]/;
a.x[i]%=;
}
}
if(a.x[a.len+]) ++a.len;
return a;
}
}a[];
struct Trie{
int t[][];
int end[];
int cnt,rt;
void init(){
memset(end,0x3f,sizeof end);
cnt=rt=;
}
void insert(int id,Num a){
int ed=a.len;
int st=max(,a.len-+);//取高位40位
int now=rt;
for(int i=ed;i>=st;i--){
if(!t[now][a.x[i]])t[now][a.x[i]]=++cnt;
now=t[now][a.x[i]];
end[now]=min(end[now],id);
}
}
int query(char *s){
int now=rt,len=strlen(s),tmp;
for(int i=;i<len;i++){
tmp=s[i]-'';
if(!t[now][tmp])return -;
now=t[now][tmp];
if(end[now]>)return -;
}
return end[now];
}
}tr;
void init(){
a[].len=;a[].x[]=;
a[].len=;a[].x[]=;
tr.insert(,a[]);
tr.insert(,a[]);
for(int i=;i<;i++){
// printf("init:%d \n",i);
int tmp=i&;
a[tmp]=a[tmp]+a[tmp^];
// for(int j=1;j<=a[tmp].len;j++)printf("%d ",a[tmp].x[j]);
// printf("%d cnt:%d\n",i,tr.cnt);
tr.insert(i,a[tmp]);
}
return;
}
char s[mxn];
int n;
int main(){
int i,j;
// clock_t a=clock();
// printf("%d\n",a);
tr.init();
init();
// clock_t b=clock();
// printf("%d\n",b);
// printf("fin: %d \n",b-a);
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%s",s);
printf("Case #%d: %d\n",i,tr.query(s));
}
return ;
}

UVa12333 Revenge of Fibonacci的更多相关文章

  1. [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci

    题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...

  2. UVA-12333 Revenge of Fibonacci(竖式加法模拟 & 字典树)

    题目: 给出一个斐波那契数字的前缀,问第一个有这个前缀的数字在斐波那契数列中是第几个. 思路: 紫书提示:本题有一定效率要求.如果高精度代码比较慢,可能会超时. 利用滚动数组和竖式加法来模拟斐波那契相 ...

  3. hdu 4099 Revenge of Fibonacci 大数+压位+trie

    最近手感有点差,所以做点水题来锻炼一下信心. 下周的南京区域赛估计就是我的退役赛了,bless all. Revenge of Fibonacci Time Limit: 10000/5000 MS ...

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

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

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

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

  6. HDU 4099 Revenge of Fibonacci Trie+高精度

    Revenge of Fibonacci Problem Description The well-known Fibonacci sequence is defined as following: ...

  7. UVA 12333 Revenge of Fibonacci

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

  8. HDU 4099 Revenge of Fibonacci(高精度+字典树)

    题意:对给定前缀(长度不超过40),找到一个最小的n,使得Fibonacci(n)前缀与给定前缀相同,如果在[0,99999]内找不到解,输出-1. 思路:用高精度加法计算斐波那契数列,因为给定前缀长 ...

  9. HDU 4099 Revenge of Fibonacci (数学+字典数)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4099 这个题目就是一个坑或. 题意:给你不超过40的一串数字,问你这串数字是Fibonacci多少的开头 ...

随机推荐

  1. shell脚本,awk取奇数行与偶数行方法。

    第一种方法: 第二种方法: 第三种方法:

  2. cocos2dx for android 接入 fmod的过程

    cocos2dx自带的音效播放有SimpleAudioEngine和AudioEngine两个,SimpleAudioEngine可以播放简单的音效, 如果播放音效数量过多的话,多导致有些音效播放失败 ...

  3. iOS应用架构谈part3 网络层设计方案

    前言 网络层在一个App中也是一个不可缺少的部分,工程师们在网络层能够发挥的空间也比较大.另外,苹果对网络请求部分已经做了很好的封装,业界的AFNetworking也被广泛使用.其它的ASIHttpR ...

  4. ajax $.post 一直报 Forbidden (CSRF token missing or incorrect.)

    由于后台整合类视图代码,所以修改了写法,完了之后用下面的写法写的post请求都报 403 error $.post( "{% url 'test_record:select_node_pag ...

  5. Docker 镜像&仓库 获取及推送镜像

    docker查看.删除镜像 docker镜像存储位置: /var/lib/docker 查看docker信息也可以查看保存位置 docker info 1.列出镜像 docker images -aa ...

  6. Python + Bottle + 谷歌搜索Api 实现简单搜索引擎

    1.运行环境 python3 centos7 2.Bottle的使用 使用bottle主要是因为它仅用python自带的库即可实现对web的搭建. bottle源码分析 bottle使用教程 3.代码 ...

  7. python爬虫用到的一些东西

    原装requests >>> import requests >>> response = requests.get('http://www.baidu.com') ...

  8. 如何用纯 CSS 创作一台拍立得照相机

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/YjYgey 可交互视频 此视频是可 ...

  9. Python中关于函数的介绍

    一.什么是函数       当我们在日常工作中编写代码时,有没有发现这种情况,写了一套代码,却发现里面有很多段代码出现了有规律的重复,这样就不符合一个合格程序员的标准了,一个合格的程序员编写的代码最重 ...

  10. 小谈python里 列表 的几种常用用法

    在python中列表的常用方法主要包括增加,删除,查看和修改.下面以举例子的方法具体说明,首先我们创建两个列表,列表是用[ ]表示的,里面的元素用逗号隔开. a=[‘hello’,78,15.6,‘你 ...