hdu1023 Train Problem II
2015-08-21 10:57
387 查看
结题思路很容易想到递归,用记忆化搜索方式寻找答案。
由于ans可能非常大,用c++需要自己写加法器。
acm.hdu.edu.cn/showproblem.php?pid=1023
View Code
由于ans可能非常大,用c++需要自己写加法器。
acm.hdu.edu.cn/showproblem.php?pid=1023
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef __int64 LL; const int maxn = 150 + 10; char buffer[maxn * maxn][maxn]; LL f[maxn][maxn]; int n, N; void add(int lhs, int rhs, int dest){ int d = 0; for(int i = maxn - 2; i >= 0; i--){ int j = buffer[lhs][i] + buffer[rhs][i] + d; buffer[dest][i] = j % 10; d = j / 10; } } int solve(int s, int q){ if(f[s][q] != -1) return f[s][q]; if(!q) return f[s][q] = 0; if(s){ int l1 = solve(s + 1, q - 1), l2 = solve(s - 1, q); add(l1, l2, N); N++; return f[s][q] = N - 1; } return f[s][q] = solve(s + 1, q - 1); } void print(int i){ int low = maxn - 1; for(int j = 0; j < maxn - 1; j++){ if(buffer[i][j]){ low = j; break; } } for(int j = low; j < maxn - 1; j++) putchar(buffer[i][j] + '0'); putchar('\n'); } int main(){ memset(f, -1, sizeof f); memset(buffer, 0, sizeof buffer); buffer[0][maxn - 2] = 1; N = 1; while(~scanf("%d", &n)){ int ans = solve(0, n); print(ans); } return 0; }
View Code
相关文章推荐
- Container With Most Water
- LightOJ-1138 Trailing Zeroes (III) (二分搜索)
- 磁盘阵列RAID理解
- time_wait的内核调优
- 你知道RAID中的Write Hole问题吗?
- 你知道RAID中的Write Hole问题吗?
- UVA 10976 Fractions Again?!
- Trailing Zeroes (III) -;lightoj 1138
- 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用
- HUST team contest #E A Mountain Road||poj 3846 (dp)
- 阻塞套接字返回EAGAIN
- 开源SIP服务器 Kamailio
- 开源SIP服务器 Kamailio
- UVA - 11374 Airport Express(dijkstra)
- 内核中返回结构体首地址的函数container_of
- 不使用main方法入口,在控制台打印“hello world”
- [翻译] EAIntroView
- LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】
- Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】
- awaitTermination() shutdown()