zoukankan      html  css  js  c++  java
  • hdu1131 Count the Trees解题报告

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1131

    Problem Description
    Another common social inability is known as ACM (Abnormally Compulsive Meditation). This psychological disorder is somewhat common among programmers. It can be described as the temporary (although frequent) loss of the faculty of speech when the whole power of the brain is applied to something extremely interesting or challenging. 
    Juan is a very gifted programmer, and has a severe case of ACM (he even participated in an ACM world championship a few months ago). Lately, his loved ones are worried about him, because he has found a new exciting problem to exercise his intellectual powers, and he has been speechless for several weeks now. The problem is the determination of the number of different labeled binary trees that can be built using exactly n different elements. 

    For example, given one element A, just one binary tree can be formed (using A as the root of the tree). With two elements, A and B, four different binary trees can be created, as shown in the figure. 

    If you are able to provide a solution for this problem, Juan will be able to talk again, and his friends and family will be forever grateful. 

     

    Input
    The input will consist of several input cases, one per line. Each input case will be specified by the number n ( 1 ≤ n ≤ 100 ) of different elements that must be used to form the trees. A number 0 will mark the end of input and is not to be processed. 
     

    Output
    For each input case print the number of binary trees that can be built using the n elements, followed by a newline character. 
     

    Sample Input
    1 2 10 25 0
     


    c(2n, n)/(n+1)c(2n, n)/(n+1)c(2n, n)/(n+1)

    Sample Output
    1 4 60949324800 75414671852339208296275849248768000000
    本题是卡特兰数的应用,因为有那个节点的二叉树可以有c(2n, n)/(n+1)种而节点又不同共有 n!种,
    所以N=c( 2n, n )/( n+1 )*n! 。

     1 #include <stdio.h>
    2 int a[105][1000];
    3 void ktl( )
    4 {
    5 int i, j, yu, len, k;
    6 a[2][0]=1;
    7 a[1][1]=1;
    8 a[1][0]=1;
    9 len=1;
    10 for( i=2; i<101; ++i )
    11 {
    12 yu=0;
    13 for( k=1; k<=i; ++k )
    14 {
    15 for( j=1; j<=len; ++j )
    16 {
    17 //int t=( a[i-1][j]+yu) * ( 4*i-2 ); wa了一下午
    18 int t=( a[i-1][j]) * ( 4*i-2 )*k+yu;
    19 yu=t/10;
    20 a[i][j]=t%10;
    21 }
    22 while( yu )
    23 {
    24 a[i][++len]=yu%10;
    25 yu/=10;
    26 }
    27 }
    28
    29 for( j=len; j>=1; --j )
    30 {
    31 int t=a[i][j]+yu*10;
    32 a[i][j]=t/( i+1 );
    33 yu= t%(i+1);
    34 }
    35 while( !a[i][len] )
    36 {
    37 len--;
    38 }
    39 a[i][0]=len;
    40 }
    41 }
    42 int main( )
    43 {
    44 ktl( );
    45 int n;
    46 while( scanf( "%d", &n ) != EOF, n )
    47 {
    48 for( int i=a[n][0]; i>0; --i )
    49 {
    50 printf( "%d", a[n][i] );
    51 }
    52 puts( "" );
    53 }
    54 }

      

  • 相关阅读:
    Linux学习笔记:常用100条命令(三)
    Linux学习笔记:常用100条命令(二)
    k8s的imagePullSecrets如何生成及使用
    Jenkins基于https的k8s配置
    ubuntu supervisor管理uwsgi+nginx
    ubuntu Django + Uwsgi + Nginx 的生产环境部署
    ubuntu 安装和配置 GitLab
    ubuntu 安装harbor仓库
    ubuntu 安装Jenkins
    php过滤html标签截取部分内容
  • 原文地址:https://www.cnblogs.com/jian1573/p/2129026.html
Copyright © 2011-2022 走看看