hdu1023 Train Problem II

结题思路很容易想到递归,用记忆化搜索方式寻找答案。

由于ans可能非常大,用c++需要自己写加法器。

acm.hdu.edu.cn/showproblem.php?pid=1023

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4
 5 using namespace std;
 6 typedef __int64 LL;
 7 const int maxn = 150 + 10;
 8 char buffer[maxn * maxn][maxn];
 9 LL f[maxn][maxn];
10 int n, N;
11
12 void add(int lhs, int rhs, int dest){
13     int d = 0;
14     for(int i = maxn - 2; i >= 0; i--){
15         int j = buffer[lhs][i] + buffer[rhs][i] + d;
16         buffer[dest][i] = j % 10;
17         d = j / 10;
18     }
19 }
20
21 int solve(int s, int q){
22     if(f[s][q] != -1) return f[s][q];
23     if(!q) return f[s][q] = 0;
24     if(s){
25         int l1 = solve(s + 1, q - 1), l2 = solve(s - 1, q);
26         add(l1, l2, N);
27         N++;
28         return f[s][q] = N - 1;
29     }
30     return f[s][q] = solve(s + 1, q - 1);
31 }
32
33 void print(int i){
34     int low = maxn - 1;
35     for(int j = 0; j < maxn - 1; j++){
36         if(buffer[i][j]){
37             low = j;
38             break;
39         }
40     }
41     for(int j = low; j < maxn - 1; j++) putchar(buffer[i][j] + ‘0‘);
42     putchar(‘\n‘);
43 }
44
45 int main(){
46     memset(f, -1, sizeof f);
47     memset(buffer, 0, sizeof buffer);
48     buffer[0][maxn - 2] = 1;
49     N = 1;
50     while(~scanf("%d", &n)){
51         int ans = solve(0, n);
52         print(ans);
53     }
54     return 0;
55 }

时间: 2024-11-13 09:47:04

hdu1023 Train Problem II的相关文章

hdu1023 Train Problem II(卡特兰数)

题目意思: http://acm.hdu.edu.cn/showproblem.php?pid=1023 求出第n个卡特兰数,n<1000. 题目分析: 很明显c(n)将很大,我们可以用大数模板,也可以用java中的大整数类,这里用到了java,将java在处理大数的时候还是很有优势的. AC代码: /** * java实现卡特兰数 * 前几项:1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786,208012- * 公式 C(n)=C(2n,n

HDU1023 Train Problem II【Catalan数】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1023 题目大意: 一列N节的火车以严格的顺序到一个站里.问出来的时候有多少种顺序. 解题思路: 典型的求Catalan数的题目,可是结果会非常大,所以须要用大数来解决. Catalan公式为 h(n) = h(n-1) * (4*n-2) / (n + 1),h(0) = h(1) = 1. AC代码: #include<iostream> #include<algorithm> #

ACM-卡特兰数之Train Problem II——hdu1023

***************************************转载请注明出处:http://blog.csdn.net/lttree*************************************** Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5589    Accept

(母函数 Catalan数 大数乘法 大数除法) Train Problem II hdu1023

Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10372    Accepted Submission(s): 5543 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Sta

Train Problem II 卡特兰裸题(入门题)

Train Problem II  题目大意:给你一个数n,表示有n辆火车,编号从1到n,从远方驶过来,问你有多少种出站的可能. 解题思路:模拟栈的问题而已.  卡特兰问题. 1 import java.math.*; 2 import java.util.*; 3 import java.io.*; 4 5 public class Main 6 { 7 static int MS=101; 8 public static void main(String[] args) 9 { 10 Sca

hdoj 1023 Train Problem II 【卡特兰】+【高精度】

题意:询问有多少种进站出站的顺序. 经典卡特兰.我对卡特兰目前的认识就是有n个1和n个-1,组成一个为2n的数列的方式有多少种.这就跟火车进站出站类似, 至于具体的卡特兰数的介绍,百度解释的很详细. 代码1(c语言): /* h(n) = h(n-1)*(4*n-2)/(n+1); */ #include <stdio.h> #include <string.h> #define M 110 int s[M][M] = {0}, b[M]; void init(){ s[1][0]

HDU——1023 Train Problem II

Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9701    Accepted Submission(s): 5210 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Stat

C - Train Problem II——(HDU 1023 Catalan 数)

传送门 Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7616    Accepted Submission(s): 4101 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train

hdu 1023 Train Problem II 这题运用到大数相乘+大数相除+卡特兰数

Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6454    Accepted Submission(s): 3514 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Stat