一道经典Dp.

定义dp[i][j] 表示由i个节点,j 层高度的累计方法数

状态转移方程为: 用i个点组成深度最多为j的二叉树的方法树等于组成左子树的方法数

乘于组成右子树的方法数再累计。

    &
/ \
@ #
/ \
@ @

如图中 & 为顶点, @ 为左子树, # 为右子树

需要注意的是,左子树的节点数目同样也为奇数

然后遍历一遍从k = 1 到 i - 2, 考虑上一层所有的左/右子树方法数再作累加

最后输出答案的时候减去上一层累加数即可

/*
ID: wushuai2
PROG: nocows
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)
#define RV(num) ((num) > 0 ? 0 : 1) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ;
const int MAX_N = ;
const int MAXSIZE = ; int N, K;
int dp[][]; int main() {
ofstream fout ("nocows.out");
ifstream fin ("nocows.in");
int i, j, k, l, m, n, t, s, c, w, q, num;
fin >> N >> K;
for(i = ; i <= N; ++i)
dp[][i] = ;
for(j = ; j <= K; ++j){
for(i = ; i <= N; ++i){
if(i & ){
for(k = ; k <= i - ; ++k){
if(k & ){
dp[i][j] = (dp[k][j - ] * dp[i - - k][j - ] + dp[i][j]) % ;
}
}
}
}
}
fout << (dp[N][K] - dp[N][K - ] + ) % << endl; fin.close();
fout.close();
return ;
}

USACO Cow Pedigrees 【Dp】的更多相关文章

  1. Kattis - honey【DP】

    Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...

  2. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  3. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  4. HDOJ 1257 最少拦截系统 【DP】

    HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  5. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  6. HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】

    HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  7. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

  8. HackerRank - common-child【DP】

    HackerRank - common-child[DP] 题意 给出两串长度相等的字符串,找出他们的最长公共子序列e 思路 字符串版的LCS AC代码 #include <iostream&g ...

  9. LeetCode:零钱兑换【322】【DP】

    LeetCode:零钱兑换[322][DP] 题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成 ...

随机推荐

  1. Git使用记录(二)

    一)git init 初始化仓库 要使用Git进行版本管理,必须先初始化仓库,请先建立一个目录并初始化仓库 mkdir gittest cd gittest git init 初始化成功以后会在当前目 ...

  2. python自学笔记(五)python文本操作

    一.python自带方法 r:read 读 w:write 写 a:append 尾行追加 先命令行进入python后 >>>d = open('a.txt','w') #在对应路径 ...

  3. jQuery File Upload

    jQuery File Upload介绍.............................................. 2 实现基本原理......................... ...

  4. Mathematica

    Mathematica是一款科学计算软件,很好地结合了数值和符号计算引擎.图形系统.编程语言.文本系统.和与其他应用程序的高级连接.很多功能在相应领域内处于世界领先地位,它也是使用最广泛的数学软件之一 ...

  5. 读书笔记:js设计模式

    面向过程编程,面向对象编程和函数式编程> 定义一个类方法1:function Anim(){ } Anim.prototype.start = function(){ .. };Anim.pro ...

  6. 射频识别技术漫谈(26)——Felica的文件系统

    Felica的文件系统使用“系统\域\服务\数据块”的结构,如下图所示.通过这种结构实现对卡片非易失性存储区的使用和操作.                                     Fe ...

  7. 射频识别技术漫谈(23)——ISO15693的载波、调制与编码

    射频识别技术中的通讯大多是主从式,主动方一般是读写器,被动方称为“卡片”或“标签”.到底是叫“卡片”还是“标签”,好像也没有严格的区分.习惯上可以从以下4个方面界定:一是形状,卡片通常体积较大,更像“ ...

  8. BZOJ 1058 报表统计 (STL)

    题解:数据结构的基本操作,用STL可以完美实现,就是比较慢…… #include <cstdio> #include <map> #include <set> #i ...

  9. Linux设置高分辨率后无法进入X系统

    Vmware9.0中Xubuntu分辨率从800x600变更为1366x768后在用户输入密码登录后会自动退出x系统,出现这种情况时可以切换到命令行登录界面,然后将-/.config/xfce4/xf ...

  10. Node.js入门-Node.js 介绍

    Node.js 是什么 Node.js 不是一种独立的语言,与 PHP,Python 等"既是语言优势平台"不同,它也不是一个 JavaScrip 框架,不同于 CakePHP,D ...