All together now number binary tree (DP)

Links: https://ac.nowcoder.com/acm/problem/13593
Source: Cattle-off network

Title Description

One day, Zzq is the data structure class. Teacher in the binary tree on top of the podium, zzq below staring.
Zzq suddenly thought of a question: For an n nodes, m a binary tree leaves, how many kinds of patterns na? Can you tell him?
For the interpretation of the first set of sample


Enter a description:

Each set of input line, two positive integers n, m (n <= 50) Significance The title

Output Description:

Each output line of a number, the corresponding answer to the inquiry indicates modulo 1000000007

Specific ideas:

O(n^4)枚举,dp[i][j]=sigma(dp[x][y]*dp[i-1-x][j-y]);

DP [i] [j] represents the current binary tree has several nodes embodiment i j leaf nodes, and then enumerate left subtree, DP [x] [y] with a left subtree of nodes x y number of leaf nodes in the program .

Note that is not a binary tree, if it is a binary tree, then it is in accordance with the method left subtree and right subtree to enumerate. Beginning to think is fixed above the node, then the tree leaf node is fixed, so, the remaining points can be random combinations of the (fight that should be thought out how the combination)

AC Code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 # define ll long long
 4 # define LL_inf (1ll<<60)
 5 # define inf 0x3f3f3f3f3
 6 const int maxn = 5e5+100;
 7 const int mod = 1e9+7;
 8 ll dp[55][55];
 9 void init()
10 {
11     dp[0][0]=1;
12     dp[1][1]=1;
13     for(int i=1; i<=50; i++)
14     {
15         for(int j=1; j<=i-1; j++)
16         {
17             for(int x=0; x<=i-1;x++)
18             {
19                 for(int y=0; y<=x; y++)
20                 {
 21 is                      IF (I- . 1 the -X-< 0 || JY < 0 )
 22 is                          Continue ;
 23 is                      DP [I] [J] = (DP [I] [J] + (DP [X] [Y]% MOD) * (DP [I- . 1 the -X-] [JY]% MOD)% MOD); // Note that this is cumulative
 24                      DP [I] [J]% = MOD;
 25                  }
 26 is              }
 27          }
 28      }
 29  }
 30  int main ()
 31 is  {
 32      int n-, m;
 33 is      the init ();
 34 is      the while(~scanf("%d %d",&n,&m))
35     {
36         printf("%lld\n",dp[n][m]);
37     }
38     return 0;
39 }

 

Guess you like

Origin www.cnblogs.com/letlifestop/p/11005725.html