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的更多相关文章

  1. USACO Section2.3 Cow Pedigrees 解题报告 【icedream61】

    nocows解题报告------------------------------------------------------------------------------------------ ...

  2. 洛谷P1472 奶牛家谱 Cow Pedigrees

    P1472 奶牛家谱 Cow Pedigrees 102通过 193提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 农民约翰准备 ...

  3. 【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 ...

  4. USACO Section 2.3 奶牛家谱 Cow Pedigrees

    OJ:http://www.luogu.org/problem/show?pid=1472 #include<iostream> using namespace std; const in ...

  5. USACO Cow Pedigrees 【Dp】

    一道经典Dp. 定义dp[i][j] 表示由i个节点,j 层高度的累计方法数 状态转移方程为: 用i个点组成深度最多为j的二叉树的方法树等于组成左子树的方法数 乘于组成右子树的方法数再累计. & ...

  6. [USACO Section 2.3] Cow Pedigrees (动态规划)

    题目链接 Solution 我DP太菜啦... 考虑到一棵二叉树是由根节点以及左儿子和右儿子构成. 所以答案其实就是 左儿子方案数*右儿子方案数 . 状态定义: \(f[i][j]\) 代表深度为 \ ...

  7. USACO 6.1 Cow XOR

    Cow XORAdrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All of ...

  8. USACO 2012 Feb Cow Coupons

    2590: [Usaco2012 Feb]Cow Coupons Time Limit: 10 Sec Memory Limit: 128 MB Submit: 349 Solved: 181 [Su ...

  9. USACO 2.4 Cow Tours

    Cow Tours Farmer John has a number of pastures on his farm. Cow paths connect some pastures with cer ...

随机推荐

  1. 网络叠加模式VLAN、VxLAN、GRE

    什么是叠加网络 1.一个数据包(或帧)封装在另一个数据包内;被封装的包转发到隧道端点后再被拆装. 2.叠加网络就是使用这种所谓"包内之包"的技术安全地将一个网络隐藏在另一个 网络中 ...

  2. SQL注入(四)

    参数绑定(预编译语句) 虽然数据库自带的过滤是个不错的实现,但是我们还是处在“用户输入被当成 SQL语句的一部分 ”这么个圈子里,其实要跳出这个圈子还有一个实现,就是参数绑定.基本上所有的主流数据库都 ...

  3. USACO 3.4 American Heritage

    American Heritage Farmer John takes the heritage of his cows very seriously. He is not, however, a t ...

  4. openstack私有云布署实践【9.2 Glance镜像管理(办公网环境)】

    首先登录controller1创建glance数据库,并赋于远程和本地访问的权限.      mysql -u root -p   CREATE DATABASE glance; GRANT ALL ...

  5. MemoryStream和FileStream

    一,FileStream对象的数据来自文件,而MemoryStream对象的数据来自内存缓冲区.这两个类都继承自Stream类. 二,抽象基类System.IO.Stream代表流,它提供Read和W ...

  6. 快速理解Parquet的DL和RL

    关于Parquet的详细介绍,请参考: 新一代列式存储格式Parquet,此文中对Parquet做了详细的介绍,这里不做重复介绍,但其中关于Definition Level(DL)和Repeated ...

  7. ubuntu16.04 samba 配置

    samba是一个很有用的在Linux和Windows之间共享文件的服务器程序,在工作的时候一直在使用,不过都是别人配置好的环境,自已一直没有配置过Samba服务器,今天尝试着自己配置的一次遇到了很多的 ...

  8. Word 2016插入公式快捷键

    实用的插入公式快捷键"Alt+=", 与君共享

  9. 在二叉搜索树(BST)中查找第K个大的结点之非递归实现

    一个被广泛使用的面试题: 给定一个二叉搜索树,请找出其中的第K个大的结点. PS:我第一次在面试的时候被问到这个问题而且让我直接在白纸上写的时候,直接蒙圈了,因为没有刷题准备,所以就会有伤害.(面完的 ...

  10. matlab中选择图片路径

    %读取训练图片数据文件 [FileName,PathName] = uigetfile('*.*','选择测试图片数据文件t10k-images.idx3-ubyte'); %暴露图片路径 saved ...