(母函数 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 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 several test cases. Each test cases consists of a number N(1<=N<=100). The input is terminated by the end of file.

Output

For each test case, you should output how many ways that all the trains can get out of the railway.

Sample Input

1

2

3

10

Sample Output

1

2

5

16796

Hint

The result will be very large, so you may not process it by 32-bit integers.

注意:

这是卡特兰数,用大数。使用的公式为:

h(n)=h(n-1)*(4*n-2)/(n+1)。

用JAVA更简单:

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        BigInteger a[]=new BigInteger[101];
        a[0]=BigInteger.ZERO;
        a[1]=BigInteger.ONE;
        for(int i=2;i<=100;i++)
            a[i]=a[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));
        while(in.hasNextInt()) {
            int n=in.nextInt();
            System.out.println(a[n]);
        }
    }
}

接下来是C++:

#include <iostream>
#include <cstdio>
using namespace std;
int a[105][105];
int i,j,n;
void ctl()         //打表。
{
    a[2][0]=1;
    a[2][1]=2;
    a[1][0]=1;
    a[1][1]=1;
    int len=1,t,yu=0;       //注意初始化。
    for(i=3;i<101;i++)         //先打表求出1到100的所有Catalan数。
    {
        for(j=1;j<=len;j++)              //大数乘法。
        {
            t=a[i-1][j]*(4*i-2)+yu;
            yu=t/10;
            a[i][j]=t%10;       //求每位数的确定的数。
        }
        while(yu)         //进位。一直到yu为0为止。
        {
            a[i][++len]=yu%10;
            yu/=10;
        }
        for(j=len;j>=1;j--)              //大数除法。联系手工除法步骤。
        {
            t=a[i][j]+yu*10;
            a[i][j]=t/(i+1);           //可以看做手工除法的商。
            yu=t%(i+1);              //可以看做手工除法的余数。
        }
        while(!a[i][len])        //去掉前边的0。
        {
            len--;
        }
        a[i][0]=len;
    }
}
int main()
{
    ctl();
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=a[n][0];i>0;i--)
        {
            printf("%d",a[n][i]);
        }
        puts("");
    }
    return 0;
} 

原文地址:https://www.cnblogs.com/Weixu-Liu/p/9164725.html

时间: 2024-12-15 01:44:27

(母函数 Catalan数 大数乘法 大数除法) Train Problem II hdu1023的相关文章

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

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

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

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

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

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

组合数学 + 大数乘法 + 大数除法 之 hdu 1261 字串数

//  [3/17/2015 JmingS] /* 此题可直接推导出公式: {(A1+A2+……+An)!} / {A1!A2!……An!} 由于 (12×26)! = 312! 只能通过数组来存储,所以又涉及『大数乘法』和『大数除法』, 大数实现的主要思想模拟手算,具体参考程序. */ 1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <cstring> 5