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 ;
}

上一篇:[转]CodeSmith和PowerDesigner的使用安装和数据库创建


下一篇:ThinkPHP配置项(六)