zoukankan      html  css  js  c++  java
  • Uva 10007 / HDU 1131

     Count the Trees 

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


    egin{picture}(400,90)(50,0)
put(115,62){A}
put(75,10){B}
put(120,60){vecto...
...}}
put(365,62){B}
put(405,10){A}
put(370,60){vector(1,-1){40}}
end{picture} 

    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 numbern ( 1 ≤ n ≤ 300 ) 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

    Sample Output 

    1
    4
    60949324800
    75414671852339208296275849248768000000

    Miguel Revilla 
    2000-08-21
     

    解题思路:result(n) = Canlatan(n)*n!

    这题完全是水过去的,给你的测试数据前两个还是可以算出来,但第三个数据以后就很大了,因为是Cantalan的一个系列,所以我试出了10的sampleOutput刚好是:10的阶乘*Cantalan数,然后计算了25的输出,发现也是如此,在原有的代码上稍加改进在杭电过了之后交到了Uva,Uva的数据比较大 n<=300

     1 #include<cstdio>
     2 #include<cstring>
     3 #define SIZE 200
     4 #define MAXN 302
     5 #define BASE 10000
     6 
     7 using namespace std;
     8 
     9 int Canlan[MAXN][SIZE];
    10 int temp[SIZE];
    11 
    12 void A(int n)
    13 {
    14     memcpy(temp, Canlan[n], sizeof(temp));
    15     int  e = 0;
    16     for(int fac = n; fac>0; --fac)
    17     {
    18         for(int i = SIZE-1, remain = 0; i>=0; --i)
    19         {
    20             e = temp[i] * fac + remain;
    21             temp[i] = e % BASE;
    22             remain = e / BASE;
    23         }
    24     }
    25     memcpy(Canlan[n], temp, sizeof(int)*SIZE);
    26 }
    27 
    28 void multiply(int elem)
    29 {
    30     for(int i=SIZE-1, remain = 0; i >= 0; --i)
    31     {
    32         remain = temp[i] * elem + remain;
    33         temp[i] = remain % BASE;
    34         remain = remain / BASE;
    35     }
    36     return;
    37 }
    38 
    39 void divide(int elem)
    40 {
    41     int i;
    42     for(i=0; i<SIZE && temp[i] == 0; i++);
    43     for(int remain = 0; i < SIZE; ++i)
    44     {
    45         remain += temp[i];
    46         temp[i] = remain / elem;
    47         remain = (remain % elem) * BASE;
    48     }
    49     return;
    50 }
    51 
    52 void Cantalan()
    53 {
    54     memset(Canlan[1], 0, sizeof(int)*SIZE);
    55     Canlan[1][SIZE-1] = 1;
    56     for(int i=2; i<MAXN; ++i)
    57     {
    58         memcpy(temp, Canlan[i-1], sizeof(int)*SIZE);
    59         multiply(4*i-2);
    60         divide(i+1);
    61         memcpy(Canlan[i], temp, sizeof(int)*SIZE);
    62     }
    63     return;
    64 }
    65 
    66 int main()
    67 {
    68     int n;
    69     Cantalan();
    70     for(int i=1; i<MAXN; ++i) A(i);
    71     while(scanf("%d", &n) != EOF && n)
    72     {
    73         int i;
    74         for(i=0; i<SIZE && Canlan[n][i] == 0; i++);
    75         printf("%d", Canlan[n][i]);
    76         if(i+1 == SIZE)
    77         {
    78             printf("
    ");
    79             continue;
    80         }
    81         for(i=i+1; i<SIZE; ++i)
    82             printf("%04d", Canlan[n][i]);
    83         printf("
    ");
    84     }
    85     return 0;
    86 }
  • 相关阅读:
    Linux部署Spingboot项目
    Linux Centos7yum安装Mysql8.0.21
    Linux配置网络yum源,提高下载速度
    Linux安装jdk1.8
    Spring的AOP
    Spring的事务管理
    Maven项目中,使用mybatis,根据数据库自动生成pojo实体类、dao、mapper
    Ubuntu14.04中使用docker容器部署tomcat镜像+java web项目
    mybatis
    spl
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/3206445.html
Copyright © 2011-2022 走看看