Count the Trees 典型卡特兰数

                      

            Count the Trees

题目分析:给你n个分别标为1,2,...,n的节点,问可以构成多少棵而叉树。

分析:首先考虑n个节点是相同的。任选一个节点做跟节点,那么剩下的n-1个节点构成跟节点的左子树和又子数。

h[n] = h[0] * h[n-1] + h[1] * h[n - 2] + ... + h[n-1] * h[0];这正是卡特蓝表达式。

h[n] = h[n-1] * (4 * n - 2) / (n + 1).   h[n] = C(2 * n,n)/(n + 1);

对于每一种二叉树,排列的话,需要乘n!.  所以答案为h[n] * n!;

 1 import java.util.*;
 2 import java.io.*;
 3 import java.math.*;
 4
 5 public class Main
 6 {
 7     public static Scanner cin = new Scanner(new BufferedInputStream(System.in));
 8     public final static int MS= 105;
 9     public final static BigInteger[] h = new BigInteger[MS];
10     public final static BigInteger[] fact = new BigInteger[MS];
11     static
12     {
13         h[0] = BigInteger.ONE;
14         h[1] = BigInteger.ONE;
15         fact[0] = BigInteger.ONE;
16         fact[1] = BigInteger.ONE;
17         for(int i = 2; i < MS; i++)
18         {
19             h[i] = h[i - 1].multiply(BigInteger.valueOf(4 * i - 2)).divide(BigInteger.valueOf(i + 1));
20             fact[i] = fact[i - 1].multiply(BigInteger.valueOf(i));
21         }
22     }
23     public static void main(String[] args)
24     {
25         int n;
26         while(cin.hasNext())
27         {
28             n = cin.nextInt();
29             if(n == 0)
30                 break;
31             System.out.println(h[n].multiply(fact[n]));
32         }
33     }
34 }
时间: 2024-11-08 17:31:15

Count the Trees 典型卡特兰数的相关文章

UVa 10007 &amp; hdu 1131 Count the Trees (卡特兰数)

Count the Trees Time Limit:3000MS    Memory Limit:0KB     64bit IO Format:%lld & %llu SubmitStatus Description  Count the Trees  Another common social inability is known as ACM (Abnormally Compulsive Meditation). This psychological disorder is somewh

UVa 10007 - Count the Trees(卡特兰数+阶乘+大数)

题目链接:UVa 10007 题意:统计n个节点的二叉树的个数 1个节点形成的二叉树的形状个数为:1 2个节点形成的二叉树的形状个数为:2 3个节点形成的二叉树的形状个数为:5 4个节点形成的二叉树的形状个数为:14 5个节点形成的二叉树的形状个数为:42 把n个节点对号入座有n!种情况 所以有n个节点的形成的二叉树的总数是:卡特兰数F[n]*n! 程序: 1 import java.math.BigInteger; 2 import java.util.Scanner; 3 public cl

hdoj-1130-How Many Trees?【卡特兰数】

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

FZU 1775 Counting Binary Trees 卡特兰数前n项和%m(m可为非素数

题目链接:点击打开链接 题意: 卡特兰数前n项和 结果%m 把答案当成2部分搞. #include<stdio.h> #include<cmath> #define int __int64 const int N = 100000; struct inverse_element{ int x, y, q; void extend_Eulid(int a,int b) { if(b == 0){ x = 1;y = 0;q = a; }else{ extend_Eulid(b,a%b

hdu 1130How Many Trees?(卡特兰数)

卡特兰数又称卡塔兰数,英文名Catalan number,是组合数学中一个常出现在各种计数问题中出现的数列. 以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)的名字来命名,其前几项为(从第零项开始) : 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 656412042

【HDU 5370】 Tree Maker(卡特兰数+dp)

Tree Maker Problem Description Tree Lover loves trees crazily. One day he invents an interesting game which is named Tree Maker. In this game, all trees are binary trees. Initially, there is a tree with only one vertex and a cursor on it. Tree Lover

[LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉排序树. 举个栗子,给定 n = 3, 共有 5 个. 1 3 3 2 1 \ / / / \ 3 2 1 1

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.括号化问题. 矩阵链乘

Buy the Ticket(卡特兰数+递推高精度)

Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1886 Accepted Submission(s): 832   Problem Description The \\\\\\\"Harry Potter and the Goblet of Fire\\\\\\\" will be on show i