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
reachable from its left has label (v) <label (k) and any node w
reachable from its right has label (w) > label (k). It is a search
structure which can find a node with label x in O(n log n) average time,
where n is the size of the tree (number of vertices).

Given a
number n, can you tell how many different binary search trees may be
constructed with a set of numbers of size n such that each element of
the set will be associated to the label of exactly one node in a binary
search tree?

Input

The input will contain a number 1 <= i <= 100 per line representing the number of elements of the set.

Output

You have to print a line in the output for each entry with the answer to the previous question.

Sample Input

1
2
3

Sample Output

1
2
5

题意:由 n个结点组成,且左节点小于父节点 右结点大于父节点的树的种数

卡特兰数+BigInteger

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        BigInteger [] h = new BigInteger[101];
        h[1] = new BigInteger("1");
        for(int i=2;i<=100;i++){
            h[i] = h[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));
        }
        Scanner sc =new Scanner (System.in);
        while(sc.hasNext()){
            int n =sc.nextInt();
            System.out.println(h[n]);
        }
    }
}
时间: 2024-10-10 16:44:30

hdu 1130(卡特兰数,大数)的相关文章

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 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

HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)

Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next few days. As a crazy fan of Harry Potter, you will go to the cinema and have the first sight, won't you? Suppose the cinema only has one ticket-office and

卡特兰数大数相乘

计算1-100卡特兰数,必须要用数组存,大数模板 注:卡特兰数:卡特兰数又称卡塔兰数,是组合数学中一个常出现在各种计数问题中出现的数列.由以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)命名.原理: 令h(1)=1,h(0)=1,catalan数满足递归式: h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (其中n>=2) 另类递归式: h(n)=((4*n-2)/(n+1))*h(n-1); 该递推关系的解为: h(n)=C(2n,n

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

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

Train Problem II (卡特兰数+大数问题)

卡特兰数: Catalan数 原理: 令h(1)=1,h(0)=1,catalan数满足递归式: h(n)= h(1)*h(n-1) + h(2)*h(n-2) + ... + h(n-1)h(1) (其中n>=2) 另类递归式: h(n)=((4*n-2)/(n+1))*h(n-1); 该递推关系的解为: h(n+1)=C(2n,n)/(n+1) (n=1,2,3,...) 最典型的四类应用:(实质上却都一样,无非是递归等式的应用,就看你能不能分解问题写出递归式了) 1.括号化问题. 矩阵链乘