HDU 4359 Easy Tree DP? (is DP but not tree DP + combo count)

Source: Internet
Author: User

HDU 4359

Test instructions: Define the Bear tree as a binary tree, this binary tree each node has a weight, the range is 2^0~2^n-1, and each value is only used once, for each node, if there are both left and right subtree, then the weight of the Zuozi and to be less than the weight of the rights subtree and. Number of Bear tree with N and Level D.

Ideas:

2^0 + 2^1 + ... + 2^n < 2^ (n+1)

According to this property, we can conclude that the maximum weight node must be on the right subtree, and as long as there are both left and right subtree, the maximum weight node on the right subtree must meet the conditions.

So we use dp[i][j] to indicate that the number of points is I and the depth does not exceed all the schemes of J, then the output is dp[n][d]-dp[n][d-1].

The composition of dp[n][d] is divided into the following two kinds:

1 is only the case of Zuozi or only right subtree, we found that only need to take any one node to do the root node, multiplied by the possible subtree (ie dp[n-1][d-1]), then the difference is Zuozi or right subtree, a total of dp[n-1][d-1] * C (n,1) * 2 cases.

2 is the case that there are both left and right subtrees, we assume that the left subtree has k nodes, then there are dp[n-k-1][d-1] "dp[k][d-1" "Left Subtree" "* C (n-2,k)" left subtree node "* C (n,1)" Root node Selection "

We add one or two to get the transfer equation, it is worth noting that, due to N, k<= 360 can be burst accuracy at any time, each operation is as far as possible modulo 10^9+7.


Code:

/** @author novicer* language:c++/c*/#include <iostream> #include <sstream> #include <fstream># include<vector> #include <list> #include <deque> #include <queue> #include <stack># include<map> #include <set> #include <bitset> #include <algorithm> #include <cstdio># include<cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime># Include<iomanip> #define INF 2147483647#define CLS (x) memset (x,0,sizeof (x)) #define RISE (I,A,B) for (int i = A; I < = b;  i++) using namespace Std;const double eps (1e-8); typedef long LONG lint;const int MAXN = 365;const int maxd = 365;const Lint MoD = 1e9 + 7;lint dp[maxn][maxd];lint c[maxn][maxd];void init () {CLS (C); memset (Dp,-1,sizeof (DP)); C[0][0] = 1;for (int i = 1; i < MAXN; i++) {c[i][0] = 1;for (int j = 1; J <= I; j + +) {C[i][j] = C[i-1][j-1] + c[i-1 ][j];//printf ("c[%d][%d]:%i64d\n", I, J, C[i][j]); if (C[i][j] > MoD) c[i][j] = mod;}}} Lint F (int n, int d) {if (n = = 1 && d >= 1) return 1;if (n = = 1 | | d = = 0) return 0;if (dp[n][d]! =-1) return dp[n][d];l int &ans = Dp[n][d];ans = (f (n-1, d-1) * c[n][1] * 2)% mod;for (int k = 1; k <= n-2; k++) ans = (ans + ((((F (n -k-1, D-1) * f (k, d-1))% MoD) * c[n-2][k])% MoD) * c[n][1])% MoD)% mod;return ans;  int main () {int t; cin >> t; int kase = 1;init (); while (t--) {int n, d;cin >> n >> d;lint ans = f (n,d)- f (n,d-1); ans = (ans + MoD)% mod;cout << "Case #" << kase++ << ":" << ans << Endl;} return 0;}


Copyright notice: Bo Master said authorized all reproduced:)

HDU 4359 Easy Tree DP? (is DP but not tree DP + combo count)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.