USACO 2.3 Cow Pedigrees
Cow Pedigrees
Silviu Ganceanu -- 2003
Farmer John is considering purchasing a new herd of cows. In this new herd, each mother cow gives birth to two children. The relationships among the cows can easily be represented by one or more binary trees with a total of N (3 <= N < 200) nodes. The trees have these properties:
- The degree of each node is 0 or 2. The degree is the count of the node's immediate children.
- The height of the tree is equal to K (1 < K < 100). The height is the number of nodes on the longest path from the root to any leaf; a leaf is a node with no children.
How many different possible pedigree structures are there? A pedigree is different if its tree structure differs from that of another pedigree. Output the remainder when the total number of different possible pedigrees is divided by 9901.
PROGRAM NAME: nocows
INPUT FORMAT
- Line 1: Two space-separated integers, N and K.
SAMPLE INPUT (file nocows.in)
5 3
OUTPUT FORMAT
- Line 1: One single integer number representing the number of possible pedigrees MODULO 9901.
SAMPLE OUTPUT (file nocows.out)
2
OUTPUT DETAILS
Two possible pedigrees have 5 nodes and height equal to 3:
@ @
/ \ / \
@ @ and @ @
/ \ / \
@ @ @ @ ————————————————————————题解
其实一眼就是dp啊……但是我dp弱成渣了
哎呀自己辣么久都没有刷USACO了呢……趁着国庆赶紧第二章结业吧……
其实一开始都没有想出来怎么搞,然后看别人题解说从下往上搞
好机智啊这样真的……然后就会了……
首先呢我们转移时相当于在两个子树上加一个根节点,但是相同高度节点数相同的子树再合并的话就一次
如果高度不同的话就两次,因为位置可以交换
枚举高度要花m个时间,然后就会T,所以我们记录个前缀和然后就可以秒过了
/*
ID: ivorysi
PROG: nocows
LANG: C++
*/ #include <iostream>
#include <string.h>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <ctime>
#include <cmath>
#include <queue>
#define ivorysi
#define mo 1000000007
#define siji(i,x,y) for(int i=(x);i<=(y);i++)
#define gongzi(j,x,y) for(int j=(x);j>=(y);j--)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);i++)
#define sigongzi(j,x,y) for(int j=(x);j>(y);j--)
#define ivory(i,x) for(int i=head[x];i;i=edge[i].n)
#define pii pair<int,int>
#define fi first
#define se second
#define inf 0x5f5f5f5f
#define N 5005
typedef long long ll;
using namespace std;
int dp[][];
int g[][];
int n,m;
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("nocows.in","r",stdin);
freopen("nocows.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
scanf("%d%d",&n,&m);
dp[][]=;
g[][]=;
siji(i,,m) {
siji(j,,n) {
if(j+>n) break;
siji(k,,n) {
if(j+k+>n) break;
dp[j+k+][i]=(1LL*dp[j+k+][i]+1LL*dp[j][i-]*dp[k][i-])%;
}
} if(i->=) {
siji(j,,n) {
if(j+>n) break;
siji(k,,n) {
if(j+k+>n) break;
dp[j+k+][i]=(1LL*dp[j+k+][i]+1LL*dp[j][i-]*g[k][i-]*)%;
}
}
}
siji(l,,n) g[l][i]=(g[l][i-]+dp[l][i])%;
//一开始这句话的位置放错了,应该这一个高度算完之后再记录,否则会比较小
}
printf("%d\n",dp[n][m]%);
return ;
}
USACO 2.3 Cow Pedigrees的更多相关文章
- USACO Section2.3 Cow Pedigrees 解题报告 【icedream61】
nocows解题报告------------------------------------------------------------------------------------------ ...
- 洛谷P1472 奶牛家谱 Cow Pedigrees
P1472 奶牛家谱 Cow Pedigrees 102通过 193提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 农民约翰准备 ...
- 【USACO 2.3】Cow Pedigrees(DP)
问n个结点深度为k且只有度为2或0的二叉树有多少种. dp[i][j]=dp[lk][ln]*dp[rk][j-1-ln],max(lk,rk)=i-1. http://train.usaco.org ...
- USACO Section 2.3 奶牛家谱 Cow Pedigrees
OJ:http://www.luogu.org/problem/show?pid=1472 #include<iostream> using namespace std; const in ...
- USACO Cow Pedigrees 【Dp】
一道经典Dp. 定义dp[i][j] 表示由i个节点,j 层高度的累计方法数 状态转移方程为: 用i个点组成深度最多为j的二叉树的方法树等于组成左子树的方法数 乘于组成右子树的方法数再累计. & ...
- [USACO Section 2.3] Cow Pedigrees (动态规划)
题目链接 Solution 我DP太菜啦... 考虑到一棵二叉树是由根节点以及左儿子和右儿子构成. 所以答案其实就是 左儿子方案数*右儿子方案数 . 状态定义: \(f[i][j]\) 代表深度为 \ ...
- USACO 6.1 Cow XOR
Cow XORAdrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All of ...
- USACO 2012 Feb Cow Coupons
2590: [Usaco2012 Feb]Cow Coupons Time Limit: 10 Sec Memory Limit: 128 MB Submit: 349 Solved: 181 [Su ...
- USACO 2.4 Cow Tours
Cow Tours Farmer John has a number of pastures on his farm. Cow paths connect some pastures with cer ...
随机推荐
- D3.js:动态效果
D3 提供了 4 个方法用于实现图形的过渡: - transition() 启动过渡效果,其前后是图形变化前后的状态(形状.位置.颜色等等),例如: .attr("fill",&q ...
- 关于Container With Most Water的求解
Container With Most Water 哎,最近心情烦躁,想在leetcode找找感觉,就看到了这题. 然而,看了题目半天,硬是没看懂,于是乎就百度了下,怕看到解题方法,就略看了下摘要,以 ...
- Fedora24 升级到25
1 安装dnf-plugin-system-upgrade dnf install dnf-plugin-system-upgrade 2 更新软件包 dnf system-upgrade downl ...
- Webupload + MVC 之上传下载
最近工作中用到了 MVC 的上传及下载 , 写下感受 本项目中用到的是百度的webuploader <!--引入Jquery--> <script src="~/Conte ...
- sqlcipher for android
github 地址 https://github.com/sqlcipher/android-database-sqlcipher 官网教程 https://www.zetetic.net/sqlci ...
- uinty3d使用ugui封装一个分页控件
我们在显示数据时有的数据比较多,手机内存有限,我们不可能分配很多的控件来显示这些数据,分页是一个不错的选择.比如玩家交易行.我们现在封装一个自己简单的分页控件来显示玩家交易行. 分页控件的原理其实很简 ...
- STM32驱动ht1621b显示LCD
这几天在写ht1621b显示LCD的程序,主芯片是Stm32f10的芯片.对于stm32和ht1621b的运用和操作本人是新手,属于赶鸭子上架,通过查看datasheet等资料和网上查看前人写的程序终 ...
- 【 VS 插件开发 】一、正确安装VS专业版
[ VS 插件开发 ]一.正确安装VS专业版
- 前端知识点-CSS相关知识点
1.解释一下CSS的盒子模型? 回答一: a.标准的css盒子模型:宽度=内容的宽度+边框的宽度+加上内边具的宽度 b.网页设计中常听的属性名:内容(content).填充(padding).边框(b ...
- 转:KVC/KVO原理详解及编程指南
作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/9674431 转载请注明出处 如果觉得文章对你有所帮助,请通过留言或 ...