UVa12333 Revenge of Fibonacci
高精度 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的更多相关文章
- [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci
题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...
- UVA-12333 Revenge of Fibonacci(竖式加法模拟 & 字典树)
题目: 给出一个斐波那契数字的前缀,问第一个有这个前缀的数字在斐波那契数列中是第几个. 思路: 紫书提示:本题有一定效率要求.如果高精度代码比较慢,可能会超时. 利用滚动数组和竖式加法来模拟斐波那契相 ...
- hdu 4099 Revenge of Fibonacci 大数+压位+trie
最近手感有点差,所以做点水题来锻炼一下信心. 下周的南京区域赛估计就是我的退役赛了,bless all. Revenge of Fibonacci Time Limit: 10000/5000 MS ...
- hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法
Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...
- HDU4099 Revenge of Fibonacci(高精度+Trie)
Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/ ...
- HDU 4099 Revenge of Fibonacci Trie+高精度
Revenge of Fibonacci Problem Description The well-known Fibonacci sequence is defined as following: ...
- UVA 12333 Revenge of Fibonacci
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 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多少的开头 ...
随机推荐
- git使用stash存储相关操作
git stash 将当前修改存储起来 git stash apply 恢复最近一次存储 git stash apply stash@{2} 恢复某一次存储 git stash list 查看存储列 ...
- $GLOBALS['HTTP_RAW_POST_DATA']与$_POST的区别
$HTTP_RAW_POST_DATA The RAW / uninterpreted HTTP POst information can be accessed with: $GLOBALS ...
- 安装cfssl证书生成工具
wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 wget https://pkg.cfssl.org/R1.2/cfssljson_linux-am ...
- 【android】android对位图文件的支持
Android 支持以下三种格式的位图文件:.png(首选)..jpg(可接受)..gif(不建议).
- Java-basic-7-面向对象
继承 在Java中,每个子类只能有一个父类,但可以继承多个接口. 子类继承父类,类定义的时候用extends. 继承接口,用implements. 重写 声明为final的方法不能被重写. 声明为st ...
- JAVA基础篇—文件上传下载
/index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...
- MIP启发式算法:Variable fixing heuristic
*本文主要记录及分享学习到的知识,算不上原创 *参考文章见链接. 本文简单介绍一下Variable fixing heuristic,这个算法同样以local search为核心框架,它的特点在于定义 ...
- MIP启发式算法:local branching
*本文主要是记录并分享最近学习到的知识,算不上原创 *参考文献见链接 本文主要是讲述local branching算法,主要以M. Fischetti的论文 “Local braching”和Pier ...
- selenium2用AutoIt上传文件
1.标签是input,如下图所示: WebElement e1= driver.findElement(By.id("load"));//输入要上传文件的地址e1.sendKeys ...
- 光学字符识别OCR-5 文本切割
经过前面文字定位得到单行的文本区域之后,我们就可以想办法将单行的文本切割为单个的字符了.因为第三步的模型是针对单个的字符建立的,因此这一步也是必须的. 均匀切割 基于方块汉字的假设,事实上最简单的切割 ...