Train Problem II

问题陈述:

  HDOJ Problem - 1023

问题解析:

  卡特兰数(Catalan)的应用

  基本性质:

  f(n) = f(1)f(n-1) + f(2)f(n-2) + ... + f(n-2)f(2) + f(n-1)f(1);

  f(n) = C(2n, n) / (n+1) = C(2n-2, n-1) / n;

   C= (4n-2)/(n+1) Cn-1

代码详解:  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <memory.h>
 4
 5 using namespace std;
 6
 7 #define MAX 101
 8 #define BASE 10000
 9
10 void multiply(int a[], int len, int b) {
11     for(int i=len-1, carry=0; i>=0; i--) {
12         carry += b * a[i];
13         a[i] = carry % BASE;
14         carry /= BASE;
15     }
16 }
17
18 void divide(int a[], int len, int b) {
19     for(int i=0, div=0; i<len; i++) {
20         div = div * BASE + a[i];
21         a[i] = div / b;
22         div %= b;
23     }
24 }
25
26 int main()
27 {
28     int i, j, h[101][MAX];
29     memset(h[1], 0, MAX*sizeof(int));
30     for(i=2, h[1][MAX-1]=1; i<=100; i++) {
31         memcpy(h[i], h[i-1], MAX*sizeof(int));
32         multiply(h[i], MAX, 4*i-2);
33         divide(h[i], MAX, i+1);
34     }
35
36     while(cin >> i && i>=1 && i <= 100) {
37         for(j=0; j<MAX && h[i][j]==0; j++);
38         printf("%d", h[i][j++]);
39         for(; j<MAX; j++)
40             printf("%04d", h[i][j]);
41         cout << endl;
42     }
43     return 0;
44 }

参考资料:

  维基百科

  CSDN

转载请注明出处:http://www.cnblogs.com/michaelwong/p/4346692.html

时间: 2024-10-29 16:17:09

Train Problem II的相关文章

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

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

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

hdoj 1023 Train Problem II 【卡特兰数】

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

(母函数 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

HDOJ 1023 Train Problem II (卡塔兰数)

Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway. Input The input contains se

Train Problem II HDU 1023 卡特兰数

Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway. Input The input contains se