首页 技术 正文
技术 2022年11月15日
0 收藏 381 点赞 3,478 浏览 1572 个字

思路:用字典树将前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();}}//insertinsert(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;} 

如有不当之处欢迎指出!

上一篇: OSI网络模型
下一篇: UVA-804 模拟
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901