UVA - 12333 字典树+大数
思路:用字典树将前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 字典树+大数的更多相关文章
- UVA - 11488 字典树
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- hdu 4099 Revenge of Fibonacci 字典树+大数
将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...
- UVa 12333 Revenge of Fibonacci (字典树+大数)
题意:给定一个长度小于40的序列,问你那是Fib数列的哪一项的前缀. 析:首先用大数把Fib数列的前100000-1项算出来,注意,一定不能是100000,要不然会WA的,然后每个数取前40位,不足4 ...
- UVA - 12333 Revenge of Fibonacci 高精度加法 + 字典树
题目:给定一个长度为40的数字,问其是否在前100000项fibonacci数的前缀 因为是前缀,容易想到字典树,同时因为数字的长度只有40,所以我们只要把fib数的前40位加入字典树即可.这里主要讨 ...
- UVa 12333 - Revenge of Fibonacci manweifc(模拟加法竖式 & 字典树)
题意: 给定n个(n<=40)数字, 求100000个以内有没有前面n个数字符合给定的数字的fibonacci项, 如果有, 给出最小的fibonacci项, 如果没有, 输出-1. 分析: 可 ...
- UVA 11732 strcmp() Anyone? (压缩版字典树)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 11488 Hyper Prefix Sets (字典树)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA Phone List (字典树)(查询是否有前缀或自身是其他的前缀)
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16341 Accepted: 5228 Descr ...
- uva 11488 - Hyper Prefix Sets(字典树)
H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...
随机推荐
- python2.7.5 安装pip
1 先安装setuptools 下载地址:https://pypi.python.org/pypi/setuptools#downloads 将下载后的tar文件解压,用CMD模式进入到解压后的文件所 ...
- Linkin大话PC常用快捷键
不管是不是程序员,常用的键盘的快捷键还是要会的,以下整理一些最常用的也比较重要的PC快捷键. 复制:CTRL+C 剪切:CTRL+X 粘贴:CTRL+V 全选:CTRL+A 撤销键:CTRL+Z 切换 ...
- Calendar使用方法
Calendar类的静态方法getInstance()可以初始化一个日历对象: Calendar now = Calendar.getInstance(); 可以使用下面三个方法把日历定到任何一个时间 ...
- 【转】对GAMIT/GLOBK的基本认识
1.1 GAMIT/GLOBK软件可从网络上申请下载.该软件功能强大,用途广泛,一般包括精确定位,大气层可降水汽估计和空间电离层变化分析等.后两种用途只需要用到GAMIT模块,精确定位则还需要GL ...
- spring mvc 中自定义404页面在IE中无法显示favicon.ico问题的解决方法。
此处用的是jsp,控制层用的是ModelAndView, 具体解决方法如下: @RequestMapping(value = "notfound", method = Reques ...
- mongodb进阶
一.游标 先插入一张表的数据 for(i=0; i<100; i++) { db.c.insert({x : i}); } 定义一个游标 var cursor = db.c.find(); 以循 ...
- python的组合数据类型及其内置方法说明
python中,数据结构是通过某种方式(例如对元素进行编号),组织在一起数据结构的集合. python常用的组合数据类型有:序列类型,集合类型和映射类型 在序列类型中,又可以分为列表和元组,字符串也属 ...
- zabbix 安装配置介绍
200 ? "200px" : this.width)!important;} --> 介绍 Zabbix是一款能够监控各种网络参数以及服务器健康性和完整性的软件.Zabbi ...
- hive权威指南<一>
一.ETL介绍: 数据抽取:把不同的数据源数据抓取过来,存到某个地方 数据清洗:过滤那些不符合要求的数据或者修正数据之后再进行抽取 不完整的数据:比如数据里一些应该有的信息缺失,需要补全后再写入数据仓 ...
- Uva 11077 Find the Permutations [置换群 DP]
题意: 给定$n$和$k$,问有多少排列交换$k$次能变成升序 $n \le 21$ $uva$貌似挂掉了$vjudge$上一直排队 从某个排列到$1,2,...,n$和从$1,2,...,n$到某个 ...