Cow Pedigrees

Farmer John is considering purchasing a new herd of cows. In this new herd, each mother cow gives birth to two children. The relationships among the cows can easily be represented by one or more binary trees with a total of N (3 <= N < 200) nodes. The trees have these properties:

  • The degree of each node is 0 or 2. The degree is the count of the node's immediate children.
  • The height of the tree is equal to K (1 < K <100). The height is the number of nodes on the longest path from the root to any leaf; a leaf is a node with no children.
    View Code
     1 /*
     2 ID:kaisada2
     3 PROG:nocows
     4 LANG:C++
     5 */
     6 #include<iostream>
     7 #include<string.h>
     8 #include<algorithm>
     9 #include<cstdio>
    10 #include<cstdlib>
    11 #include<cstring>
    12 
    13 using namespace std;
    14 
    15 int n,k;
    16 
    17 int f[200][100];
    18 
    19 int main( )
    20 {
    21     freopen("nocows.in","r",stdin);
    22     freopen("nocows.out","w",stdout);
    23     cin>>n>>k;
    24     for(int i=1;i<=k;i++)
    25     {
    26        f[1][i]=1;
    27     }
    28     for(int i=1;i<=n;i++)
    29     {
    30        for(int j=1;j<=k;j++)
    31        {
    32           for(int q=1;q<=i-2;q++)
    33           {
    34               f[i][j]+=f[q][j-1]*f[i-1-q][j-1];
    35               f[i][j]=f[i][j]%9901;
    36           }
    37        }
    38     }
    39     int ok=(f[n][k]-f[n][k-1]+9901)%9901;
    40     cout<<ok<<endl;
    41     return 0;
    42 }

How many different possible pedigree structures are there? A pedigree is different if its tree structure differs from that of another pedigree. Output the remainder when the total number of different possible pedigrees is divided by 9901.

PROGRAM NAME: nocows

INPUT FORMAT

  • Line 1: Two space-separated integers, N and K.

SAMPLE INPUT (file nocows.in)

5 3

OUTPUT FORMAT

  • Line 1: One single integer number representing the number of possible pedigrees MODULO 9901.

SAMPLE OUTPUT (file nocows.out)

2

OUTPUT DETAILS

Two possible pedigrees have 5 nodes and height equal to 3:

           @                   @      
          / \                 / \
         @   @      and      @   @
        / \                     / \
       @   @                   @   @

这道题是个DP,典型的树形DP
将每个树从根节点处一分为二,然后进行他们总类数的相乘,这道题我本来考虑到了一点,会不会存在我求出的树某个节点的儿子只有一个??
但是我后来发现,如果出现这种状况,它的节点数必然为偶数,所以不用分类讨论了
这个状态的定义是用i个节点,最多j层的数有多少种可能性
看来以后得多用这种“最多”之类的状态了,相当好用
原文地址:https://www.cnblogs.com/spwkx/p/2614267.html