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多少的开头 ...
随机推荐
- 用户价值模型 CITE :https://www.jianshu.com/p/34199b13ffbc
RFM用户价值模型的原理和应用 ▌定义 在众多的用户价值分析模型中,RFM模型是被广泛被应用的:RFM模型是衡量客户价值和客户创利能力的重要工具和手段,在RFM模式中,R(Recency)表示客户购 ...
- 如何用纯 CSS 创作一颗逼真的土星
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/EpbaQX 可交互视频 ...
- VNC远程登录端使用经验之一
1.vnc/xmanager都是经常用的远程登录软件.vnc有个缺点就是他的进程不会自动退出比如如果开了PID1再去开PID2...PIDn.那么前面的PIDn-1个进程就会一直运行如果不手动kill ...
- python日记整理
都是自己的学习总结,要是总结的有问题大佬麻烦评价一下我好修改,谢谢 python插件插件+pycharm基本用法+markdown文本编写+jupyter notebook的基本操作汇总 一.计算机基 ...
- selenium+phantomjs爬取京东商品信息
selenium+phantomjs爬取京东商品信息 今天自己实战写了个爬取京东商品信息,和上一篇的思路一样,附上链接:https://www.cnblogs.com/cany/p/10897618. ...
- Python学习笔记:py2exe打包Python程序
使用py2exe将一个Python程序打包成一个exe程序,这样Python程序也可以在没有安装Python的环境中运行Python程序了.使用这个工具需要写一个用于打包的setup.py文件(名称可 ...
- 20181206(re,正则表达式,哈希)
1.re&正则表达式 2.hashlib 一:re模块&正则表达式 正则:正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描 ...
- web开发框架tornado
在server.py文件中 import tornado.web import tornado.ioloop import tornado.httpserver import config from ...
- LeetCode(172)Factorial Trailing Zeroes
题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- 最长公共子序列(LCS)问题
最长公共子串(Longest Common Substirng)和最长公共子序列(Longest Common Subsequence,LCS)的区别为:子串是串的一个连续的部分,子序列则是从不改变序 ...