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

      

  • 相关阅读:
    使用require.context引入模块
    npm link的使用
    mysql 链接远程数据库
    微信错误:errcode=45015, errmsg=response out of time limit or subscription is canceled
    微信公众号发送模板消息
    VS CODE 开发php实现断点调试
    uni 蓝牙 安卓 监听不到返回数据 不可写入
    vue3.0 兄弟组件传值
    二叉查找树的实现和删除
    模块二:ES新特性与TypeScript、JS性能优化
  • 原文地址:https://www.cnblogs.com/jian1573/p/2129026.html
Copyright © 2020-2023  润新知