思路:用字典树将前40个数字记录下来,模拟大数加法即可。

AC代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 10;
struct Node{
	int num;
	Node *next[maxn];
	Node(){
		num = -1;
		for(int i = 0; i < maxn; i++) {
			next[i] = NULL;
		}
	}
}*root;
void init() {
	root = new Node();
}
void insert(int *a, int n, int num) {
	n = min(n, 40);
	Node *p = root, *q;
	for(int i = 0; i < n; ++i) {
		int x = a[i];
		if(p->next[x] == NULL) {
			q = new Node();
			q->num = num;
			p->next[x] = q;
		}
		p = p->next[x];
	}
}

int find(char *a) {
	Node *p = root;
	int n = strlen(a);
	for(int i = 0; i < n; ++i) {
		int x = a[i] - '0';
		if(p->next[x] == NULL) return -1;
		p = p->next[x];
	}
	return p->num;
} 

//高精度加法
const int Base =  100000000;
int a[2][20000+5], val[100];
int f = 0, h = 1;
stack<int>sta;
void Deal() {
	memset(a, 0, sizeof(a));
	a[0][0] = 1, a[1][0] = 1;
	val[0] = 1;
	int len[2] = {1,1};
	insert(val, 1, 0);
	for(int i = 2; i < 100000; ++i) {
		int g = 0;
		for(int j = 0; j < len[f] || j < len[h] || g > 0; ++j) {
			int x = a[f][j] + a[h][j] + g;
			a[f][j] = x % Base;
			g = x / Base;
			len[f] = max(len[f], j+1);
		}
		int cur = 0;
		for(int j = len[f]-1; j >= 0; --j) {
			if(cur >= 40) break;
			int x = a[f][j];
			if(j == len[f] - 1) {
				while(x) {
					sta.push(x % 10);
					x /= 10;
				}
			}
			else {
				int u = 0, t = x;
				while(t) {
					++u;
					t /= 10;
				}
				while(x) {
					sta.push(x % 10);
					x /= 10;
				}
				for(int k = 0; k < 8 - u; ++k) {
					sta.push(0);
				}
			}
			while(!sta.empty()) {
				val[cur++] = sta.top();
				sta.pop();
			}
		}
		//insert
		insert(val, cur, i);
		f = 1 - f;
		h = 1 - h;
	}
}

int main() {
	init();
	Deal();
	int T, kase = 1;
	scanf("%d", &T);
	char s[50];
	while(T--) {
		scanf("%s", s);
		printf("Case #%d: %d\n", kase++, find(s));
	}
	return 0;
} 

如有不当之处欢迎指出!

UVA - 12333 字典树+大数的更多相关文章

  1. UVA - 11488 字典树

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

  2. hdu 4099 Revenge of Fibonacci 字典树+大数

    将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...

  3. UVa 12333 Revenge of Fibonacci (字典树+大数)

    题意:给定一个长度小于40的序列,问你那是Fib数列的哪一项的前缀. 析:首先用大数把Fib数列的前100000-1项算出来,注意,一定不能是100000,要不然会WA的,然后每个数取前40位,不足4 ...

  4. UVA - 12333 Revenge of Fibonacci 高精度加法 + 字典树

    题目:给定一个长度为40的数字,问其是否在前100000项fibonacci数的前缀 因为是前缀,容易想到字典树,同时因为数字的长度只有40,所以我们只要把fib数的前40位加入字典树即可.这里主要讨 ...

  5. UVa 12333 - Revenge of Fibonacci manweifc(模拟加法竖式 & 字典树)

    题意: 给定n个(n<=40)数字, 求100000个以内有没有前面n个数字符合给定的数字的fibonacci项, 如果有, 给出最小的fibonacci项, 如果没有, 输出-1. 分析: 可 ...

  6. UVA 11732 strcmp() Anyone? (压缩版字典树)

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

  7. UVA 11488 Hyper Prefix Sets (字典树)

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

  8. UVA Phone List (字典树)(查询是否有前缀或自身是其他的前缀)

    Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16341   Accepted: 5228 Descr ...

  9. uva 11488 - Hyper Prefix Sets(字典树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

随机推荐

  1. 可等待计时器添加APC测试

    可等待计时器和用户计时器的最大区别在于用户计时器需要在应用程序中使用大量的用户界面基础设施,从而消耗更多的资源.此外,可等待计时器是内核对象,这意味着他们不仅可以在多个线程间共享,而且具备安全性. 用 ...

  2. 移动端 -webkit-user-select:text; ios10 bug 解决方案

    移动端一般body的css.会设置 作用就不解释了: body{ height:100%;min-height:100%; font-family: "微软雅黑",'Helveti ...

  3. Maven以及在Maven在Myeclipse中的配置

    一.maven安装与配置1.到官网http://maven.apache.org/download.cgi下载maven压缩包,解压到指定文件夹.如:D:\apache-maven-3.3.92.添加 ...

  4. 在Web.config中配置handler

    在Web.config中配置handler节点时发现用vs2010和用vs2015竟然不一样,经过多次测试发现了一些倪端: <configuration> <!--vs2010中需要 ...

  5. React-Native 之 Modal介绍与使用

    前言 遗漏的常用组件,刚发现官方有提供,这边也来介绍一下. 如本文有错或理解偏差欢迎联系我,会尽快改正更新! 如有什么问题,也可直接通过邮箱 277511806@qq.com 联系我. 本章涉及资源下 ...

  6. javascript中的BOM对象

    1.window对象 所有的浏览器都支持window对象 概念上讲,一个html文档对应一个window对象 功能上讲,控制浏览器窗口 使用上讲,window对象不需要创建对象,直接使用 2.wind ...

  7. 给php加速安装APC

    说明:APC-3.1.13 适应于 php-5.4.27 下载: wget http://blog.xinfilm.com/softdir/APC-3.1.13.tgz tar -zxvf APC-3 ...

  8. 利用QuickCHM制作chm

    CHM是一种常见的帮助文件格式,也是电子书的一种格式. 下面是使用QuickCHM制作chm的步骤: 1.先将所有的word文档存储为mht格式,点击,文件--另存为网页,如下 2.确保所有的word ...

  9. UOJ #274. 【清华集训2016】温暖会指引我们前行 [lct]

    #274. [清华集训2016]温暖会指引我们前行 题意比较巧妙 裸lct维护最大生成树 #include <iostream> #include <cstdio> #incl ...

  10. ASP.NET Core 入门

    关于ASP.NET Core ASP.NET Core 是一个全新的开源.跨平台框架,可以用它来构建基于网络连接的现代云应用程序,比如:Web 应用,IoT(Internet Of Things,物联 ...