高精度 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. ★iOS 性能测试工具 SDK

    一.概括 1. 做一个类似GT的性能测试工具: 2. 第一期主要是CPU.内存功能,要求可以绘制曲线,可以选择曲线区间,自动计算最小值.最大值.均值等,支持曲线全屏显示 目标的视觉效果是类似股票走势图 ...

  2. presenting view controller

    Present ViewController详解 Present ViewController Modally 一.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIK ...

  3. Clang提供的办法

    1.方法弃用警告 #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarati ...

  4. 转 救命的教程 anaconda下载安装包网络错误的解决办法

    折腾了一天,终于找到了这个解决办法 https://blog.csdn.net/sinat_29315697/article/details/80516498

  5. java版RSA工具类

    /** * RSA算法加密/解密工具类 */ public class RSAUtils { private static final Logger LOGGER = LoggerFactory.ge ...

  6. Tcl/Cmds

  7. paper:synthesizable finit state machine design techniques using the new systemverilog 3.0 enhancements之fsm summary

    主要是1.不要用1段式写FSM 2.不要用状态编码写one-hot FSM ,要用索引编码写one-hot FSM.

  8. java web项目(spring项目)中集成webservice ,实现对外开放接口

    什么是WebService?webService小示例 点此了解 下面进入正题: Javaweb项目(spring项目)中集成webservice ,实现对外开放接口步骤: 准备: 采用与spring ...

  9. LeetCode(23)Merge k Sorted Lists

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  10. LeetCode(258) Add Digits

    题目 Given a non-negative integer num, repeatedly add all its digits until the result has only one dig ...