USACO 2.3 Cow Pedigrees 题解

5 篇文章 0 订阅
4 篇文章 0 订阅

Cow Pedigrees

FROM: Silviu Ganceanu – 2003
DESCRIPTION:
    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      @   @
    / \                     / \
   @   @                   @   @
THOUGHTS:

首先根据题目描述,我们可以得到这棵二叉树的特点。对于任何一个节点,要么有两个孩子,要么没有孩子。也就是说,这种树的节点数一定是奇数(刨去第一层的1个,剩下的每层都是偶数个)。
输出是拥有n个节点k层的该类型二叉树(后简称二叉树)mod 9901的数。
我们可以发现对于k层,k-1层,k-2层…的二叉树性质是相同的,因此第一反应是动态规划(dp),那么转移方程是什么呐?
定义一个二维数组F[i][j],表示拥有i的节点最大深度为j的二叉树的个数,注意是最大深度为j,不是深度为j。
那么对于拥有i的节点深度为j的二叉树的个数,可以表示为F[i][j] - F[i][j - 1]。
最大深度为j的减去最大深度为j-1的拥有i个节点的二叉树的个数就是正好拥有i个节点且深度为j的二叉树的个数。
当然要注意一点细节,为了避免溢出,我们在运算的过程中进行取模运算,可能会出现F[i][j - 1] > F[i][j]的的情况,因此应该表示为: (F[i][j] - F[i][j - 1] + MOD) % MOD。

for k = 1 to i - 2 do
        F[i, j] = (F[i, j] + F[k, j - 1] * F[i - 1 - k, j - 1]) mod 9901;

对于任何一棵节点数为n的二叉树,都可分成一个节点数为k(k < n)的二叉树与一个节点数为n - k - 1的二叉树。两个子二叉树的节点数之和为n - 1的原因是去掉了第一层的那一个,这是必须存在的(用于增加一层深度)。
通过乘法原理,拥有a个节点的二叉树和拥有b个节点的二叉树能组成a * b个新二叉树。
那么转移方程就很清晰了:对于深度最大为j,节点数为i的二叉树,它的数量为深度最大为j - 1(刨去第一层),节点数为k(k = 1 -> i - 2)的二叉树的数目与节点数为i - 1- k的二叉树的数目之积的和。
一个微小的优化:我们发现k在从1到i-2的过程中,会出现“算两次”的情况,比如F[1][j- 1] * F[i - 2][j -1],然后当k = i - 2时的运算式与上式相同,因此可以把循环范围砍到1 - i / 2。当然如此则需要注意i是奇数的时候,F[i / 2][j - 1] * F[i / 2][j - 1]只算了一遍,需要提前减去以免多算。

TESTING POINTS: dynamic programming & multiplication principle
DIFFICULTIES: tg
AC PROGRAM:
/*
ID: kongse_1
PROG: nocows
LANG: C++
*/

// Skq_Liao

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (register int i = (a), i##_end_ = (b); i < i##_end_; ++i)
#define ROF(i, a, b) for (register int i = (a), i##_end_ = (b); i > i##_end_; --i)
#define debug(...) fprintf(stderr, __VA_ARGS__)

const char Fin[] = "nocows.in";
const char Fout[] = "nocows.out";

void In()
{
    freopen(Fin, "r", stdin);
    freopen(Fout, "w", stdout);
    return ;
}

const int MOD = 9901;

int F[200][100];

int main()
{
    In();
    int n, m;   
    scanf("%d%d", &n, &m);
    if(n & 1 == 0)
    {
        printf("0\n");
        return 0;
    }
    FOR(i, 1, m + 1)
        F[1][i] = 1;
    FOR(j, 1, m + 1)
        for(int i = 1; i < n + 1; i += 2)
        {
            if(i & 1)
                F[i][j] = (F[i][j] + MOD - F[i / 2][j - 1] * F[i / 2][j - 1]) % MOD;
            FOR(k, 1, i / 2 + 1)
                F[i][j] = (F[i][j] + 2 * F[k][j - 1] * F[i - 1 - k][j - 1]) % MOD;
        }       
    printf("%d\n", (F[n][m] + MOD - F[n][m - 1]) % MOD);
    return 0;
}

Skq_Liao 2017/07/11 8 : 54 于 AB217

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值