USACO 2.3 Cow Pedigrees (nocows)

//Main idea: 
//Dynamic Programming
//dp[i][j] denote the number of binary trees whose node number is i and height is less or equal to j;
//state transition equation:
//dp[i][j] = dp[m][j-1]*dp[i-1-m][j-1];
//dp[m][j-1] denote the left subtree with m nodes and no more than j-1 height ;
//and dp[i-1-m][j-1] denote the right subtree i-1-m nodes(the root take 1 node and left subtree take m),
//no more than j-1 height;
//So the number of birnary trees with i nodes and j height is dp[i][j]-dp[i][j-1];

/*
ID: haolink1
PROG: nocows
LANG: C++
*/

//#include <iostream>
#include <fstream>

using namespace std;

int dp[200][100];

int main(){
    ifstream fin("nocows.in");
    int node_num, hight;
    fin >> node_num >> hight;
    //boundary conditions
    for(int j = 1; j <= hight; j++){
        dp[1][j] = 1;
    }
    for(int i = 2; i <= node_num; i++){
        for(int j = 2; j <= hight; j++){
            for(int m = 1; m <= i-2; m++){
                //mod dp[i][j] by 9901 in case it exceed the int max positive value  and become negative;
                dp[i][j] = (dp[i][j] + dp[m][j-1]*dp[i-1-m][j-1])%9901;
            }
        }
    }
    ofstream fout("nocows.out");
    //Note: after mod operation, dp[node_num][hight] may be less than dp[node_num][hight-1];
    //So we add 9901 at the end;
    fout << (dp[node_num][hight] - dp[node_num][hight-1] + 9901)%9901<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值