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 }

 

转载于:https://www.cnblogs.com/hutaishi/p/4703282.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值