USACO Cow Pedigrees 【Dp】
一道经典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】的更多相关文章
- Kattis - honey【DP】
Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- HDOJ 1501 Zipper 【DP】【DFS+剪枝】
HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDOJ 1257 最少拦截系统 【DP】
HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDOJ 1159 Common Subsequence【DP】
HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】
HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】
POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...
- HackerRank - common-child【DP】
HackerRank - common-child[DP] 题意 给出两串长度相等的字符串,找出他们的最长公共子序列e 思路 字符串版的LCS AC代码 #include <iostream&g ...
- LeetCode:零钱兑换【322】【DP】
LeetCode:零钱兑换[322][DP] 题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成 ...
随机推荐
- MVC-05 Model(2)
五.使用Code First数据库迁移 当Entity Framework Code First的数据模型发生异动时,默认会引发一个System.InvalidOpertaionException异常 ...
- Java并发编程总结2——慎用CAS(转)
一.CAS和synchronized适用场景 1.对于资源竞争较少的情况,使用synchronized同步锁进行线程阻塞和唤醒切换以及用户态内核态间的切换操作额外浪费消耗cpu资源:而CAS基于硬件实 ...
- Python CSV文件处理/读写及With as 用法
可以不使用CSV模块 逐行处理: for line in open("samples/sample.csv"): title, year, director = line.spli ...
- perl5 第十二章 Perl5中的引用/指针
第十二章 Perl5中的引用/指针 by flamephoenix 一.引用简介二.使用引用三.使用反斜线(\)操作符四.引用和数组五.多维数组六.子程序的引用 子程序模板七.数组与子程序八.文件句 ...
- kinect for windows - SkeletonBasics-D2D详解之一
之前的文章介绍了深度图的获取,但是深度图只是提供了一些数据,这些数据给上层应用使用还是偏底层一些,我们希望在这个基础上,获取一些信息,比如手挥动,人跑步,或者运动等等,那么这个文章开始我们来讲述kin ...
- HDU 1720 A+B Coming
#include <string> #include <cstdio> #include <iostream> using namespace std; int c ...
- 权限控制框架Spring Security 和Shiro 的总结
关于权限控制,一开始感觉比较难,后来先是接触了Spring Security 学起来也比较吃力,再是学习了Shiro,感觉简单很多. 总体来说这些框架,主要做了两个事情 Authentication: ...
- Oracle 动态查询,EXECUTE IMMEDIATE select into使用方法
create or replace procedure TEST_TABLE is l_table_name varchar2(50); query_stat varchar2(100); l_zyq ...
- GDI+入门——带你走进Windows图形的世界
一.GDI+基础 1.GDI+简单介绍 GDI+是微软的新一代二维图形系统,它全然面向对象,要在Windows窗口中显示字体或绘制图形必需要使用GDI+.GDI+提供了多种画笔.画刷.图像等图形对象, ...
- US/OS2之任务同步与通信
嵌入式系统中的各个任务都是以并发的方式来运行的,并为同一个大的任务服务,它们不可避免地要共同使用一些共享资源,并且在处理一些需要多个任务共同协作来完成的工作时,还需要相互的支持和限制.因此,对于一个完 ...