hdu 1023 卡特兰数

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[105];
        for(int i = 0;i < 105;i++){
            a[i] = new BigInteger("1");
        }

        a[0] = BigInteger.ONE;
        for(int i = 1;i <= 101;i++){
            a[i] = a[i-1];
            for(int j = 1;j < i;j++){
                a[i] = a[i].add( a[j].multiply(a[i-j-1]));
            }
        }

        while(in.hasNext()){
            int n = in.nextInt();
            System.out.println(a[n]);
        }
    }

}
d[n]的含义是n个数的方法,明显如果在n-1的后面添加一个比这n-1个数都要大的数那么n-1个数的方法都可以存在n个数的方法中,只要把最后一个数最后出栈就行了。
然后将这n个数分成两个部分,则有则有前k个数输出之后的方法和后i-1-k的方法的乘积就是这个这种分法的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-25 05:40:25

hdu 1023 卡特兰数的相关文章

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

hdu 1023 卡特兰数+高精度

Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) 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-increasi

2014年百度之星程序设计大赛 - 初赛(第一轮) hdu Grids (卡特兰数 大数除法取余 扩展gcd)

题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展gcd, 不是用逆元吗.. 网上还有别人的解释,没看懂,贴一下: (a / b) % m = ( a % (m*b)) / b 笔者注:鉴于ACM题目特别喜欢M=1000000007,为质数: 当gcd(b,m) = 1, 有性质: (a/b)%m = (a*b^-1)%m, 其中b^-1是b模m的逆

HDU 4828 (卡特兰数+逆元)

HDU 4828 Grids 思路:可以转化为卡特兰数,先把前n个人标为0,后n个人标为1,然后去全排列,全排列的数列,如果每个1的前面对应的0大于等于1,那么就是满足的序列,如果把0看成入栈,1看成出栈,那么就等价于n个元素入栈出栈,求符合条件的出栈序列,这个就是卡特兰数了.然后去递推一下解,过程中需要求逆元去计算 代码: #include <stdio.h> #include <string.h> const int N = 1000005; const long long M

hdu 5184 卡特兰数

hdu 5184 卡特兰数 题意: 我们给出下列递归的合法括号序列的定义: 1. 空序列是合法括号序列 2. 如果s是一个合法括号序列,那么(s)也是合法括号序列 3. 如果a和b是合法括号序列,那么ab也是合法括号序列 4. 没有其它情况是合法括号序列 比如下列括号序列是合法括号序列 (), (()), ()(), ()(()) 下列括号序列则不是 (, ), )(, ((), ((() 现在,我们要构造长度为n的合法括号序列,前面的一些括号已经给出,问可以构造出多少合法序列. 限制: 1 <

HDU 1023 Catalan数+高精度

链接:HDU 1023 /**************************************** * author : Grant Yuan * time : 2014/10/19 15:51 * source : HDU 1023 * algorithm : Catalan数+高精度 * ***************************************/ import java.io.*; import java.math.*; import java.util.*;

HDU 4828 (卡特兰数+逆)

HDU 4828 Grids 思路:能够转化为卡特兰数,先把前n个人标为0.后n个人标为1.然后去全排列,全排列的数列.假设每一个1的前面相应的0大于等于1,那么就是满足的序列,假设把0看成入栈,1看成出栈.那么就等价于n个元素入栈出栈,求符合条件的出栈序列,这个就是卡特兰数了. 然后去递推一下解,过程中须要求逆元去计算 代码: #include <stdio.h> #include <string.h> const int N = 1000005; const long long

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 1130(卡特兰数,大数)

How Many Trees? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3382    Accepted Submission(s): 1960 Problem Description A binary search tree is a binary tree with root k such that any node v re