Easy Tree DP?

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1487    Accepted Submission(s): 567

Problem Description
A Bear tree is a binary tree with such properties : each node has a value of 20,21…2(N-1)(each number used only once),and for each node ,its left subtree’s elements’ sum<its right subtree’s elements’ sum(if the node hasn’t left/right subtree ,this limitation is invalid).
You need to calculate how many Bear trees with N nodes and exactly D deeps.
 
Input
First a integer T(T<=5000),then T lines follow ,every line has two positive integer N,D.(1<=D<=N<=360).
 
Output
For each test case, print "Case #t:" first, in which t is the number of the test case starting from 1 and the number of Bear tree.(mod 109+7)
 
Sample Input
2
2 2
4 3
 
Sample Output
Case #1: 4
Case #2: 72
 
Author
smxrwzdx@UESTC_Brightroar
 
Source

解题:哎。。连续训练了两个月,身累心更累。。不想说什么了,看这位大神的解说吧

他的钻头是可以突破天际的钻头

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
typedef long long LL;
const LL mod = 1e9 + ;
LL dp[maxn][maxn],c[maxn][maxn];
void init(){
memset(dp,-,sizeof dp);
for(int i = ; i < maxn; ++i){
c[i][] = c[i][i] = ;
for(int j = ; j < i; ++j)
c[i][j] = (c[i-][j-] + c[i-][j])%mod;
}
}
LL dfs(int n,int d){
if(n == && d >= ) return ;
if(n == || d == ) return ;
if(dp[n][d] != -) return dp[n][d];
LL &ret = dp[n][d];
ret = (dfs(n-,d-)*c[n][]*)%mod;
for(int k = ; k <= n-; ++k)
ret = (ret + (dfs(n-k-,d-)*dfs(k,d-)%mod*c[n-][k]%mod*c[n][])%mod)%mod;
return ret;
}
int main(){
int kase,cs = ,n,d;
init();
scanf("%d",&kase);
while(kase--){
scanf("%d%d",&n,&d);
printf("Case #%d: %I64d\n",cs++,(dfs(n,d) - dfs(n,d-) + mod)%mod);
}
return ;
}

HDU 4359 Easy Tree DP?的更多相关文章

  1. HDU 4359——Easy Tree DP?——————【dp+组合计数】

    Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. HDU 4359 Easy Tree DP? 带权二叉树的构造方法 dp

    题意: 给定n deep 1.构造一个n个节点的带权树,且最大深度为deep,每一个节点最多仅仅能有2个儿子 2.每一个节点的值为2^0, 2^1 ··· 2^(n-1)  随意两个节点值不能同样 3 ...

  3. HDU 4359 Easy Tree DP? 组合数学+动归

    题意:定义一种树,每个节点的权值都是20到2n-1,每个权值出现一次,每个节点的左子树的权值和小于右子树,除非只有一个子树.给你n和d,问有n个节点且恰好深度是d的这种树有多少种. 比赛的时候我没有做 ...

  4. HDU 5513 Efficient Tree

    HDU 5513 Efficient Tree 题意 给一个\(N \times M(N \le 800, M \le 7)\)矩形. 已知每个点\((i-1, j)\)和\((i,j-1)\)连边的 ...

  5. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  6. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. 96. Unique Binary Search Trees (Tree; DP)

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  8. HDU 4778 状压DP

    一看就是状压,由于是类似博弈的游戏.游戏里的两人都是绝对聪明,那么先手的选择是能够确定最终局面的. 实际上是枚举最终局面情况,0代表是被Bob拿走的,1为Alice拿走的,当时Alice拿走且满足变换 ...

  9. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

随机推荐

  1. BNU 13289 Energetic Pandas DP

     Energetic Pandas  There are n bamboos of different weights Wi. There are n pandas of different capa ...

  2. Why is try {…} finally {…} good; try {…} catch{} bad?

    http://stackoverflow.com/questions/128818/why-is-try-finally-good-try-catch-bad The big difference i ...

  3. wesome-android

    awesome-android Introduction android libs from github System requirements Android Notice If the lib ...

  4. A Reusable Aspect for Memory Allocation Checking

    The checking logic would be refactored into an aspect file, as follows: after(void * s) : (call($ ma ...

  5. PCB MS SQL跨库执行SQL 获取返回值

    一.SQL跨库执行SQL 获取返回值 ) DECLARE @sql nvarchar(MAX) DECLARE @layer INT SET @Dblink = 'P2.fp_db.dbo.' sel ...

  6. 原生JS---3

    原生js学习笔记3——数组 定义数组 两种方式定义一个数组: 1. var array1 = new array(1, 2, 3, 4); 2. var array2 = [1, 2, 3, 4]; ...

  7. [BZOJ1821][JSOI2010]部落划分

    感觉学了这么久还是有那么一丢丢进步的...上个学期看到这道题,虽然早就学过并查集和二分了但还是一点思路都没有,现在可以秒切了呢 思路就是二分+并查集,有些人说是生成树,其实它没有变成树,只是运用了生成 ...

  8. BZOJ 4517 组合数+错排

    思路: 预处理错排 然后C(n,m)*s[n-m-1]就是答案了 特判n-m-1<0 //By SiriusRen #include <cstdio> using namespace ...

  9. C#利用ICSharpCode将远程文件打包并下载

    应用于ASP.NET MVC中 方法主体代码: public void GetFilesByOrder(string Order_ID, string IntNumber) { MemoryStrea ...

  10. 自学Python九 爬虫实战二(美图福利)

    作为一个新世纪有思想有文化有道德时刻准备着的屌丝男青年,在现在这样一个社会中,心疼我大慢播抵制大百度的前提下,没事儿上上网逛逛YY看看斗鱼翻翻美女图片那是必不可少的,可是美图虽多翻页费劲!今天我们就搞 ...