HDU 4472(树+递归)

Description
Prof. Tigris is the head of an archaeological team who is currently in charge of an excavation in a site of ancient relics.
This site contains relics of a village where civilization once flourished. One night, examining a writing record, you find some text meaningful to you. It reads as follows.
“Our village is of glory and harmony. Our relationships are constructed in such a way that everyone except the village headman has exactly one direct boss and nobody will be the boss of himself, the boss of boss of himself, etc. Everyone expect the headman is considered as his boss’s subordinate. We call it relationship configuration. The village headman is at level 0, his subordinates are at level 1, and his subordinates’ subordinates are at level 2, etc. Our relationship configuration is harmonious because all people at same level have the same number of subordinates. Therefore our relationship is …”
The record ends here. Prof. Tigris now wonder how many different harmonious relationship configurations can exist. He only cares about the holistic shape of configuration, so two configurations are considered identical if and only if there’s a bijection of n people that transforms one configuration into another one.
Please see the illustrations below for explanation when n = 2 and n = 4.
这里写图片描述
The result might be very large, so you should take module operation with modules 10 9 +7 before print your answer.

Input
There are several test cases.
For each test case there is a single line containing only one integer n (1 ≤ n ≤ 1000).
Input is terminated by EOF.

Output
For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is the desired answer.

Sample Input
1
2
3
40
50
600
700

Sample Output
Case 1: 1
Case 2: 1
Case 3: 2
Case 4: 924
Case 5: 1998
Case 6: 315478277
Case 7: 825219749

给一个数n,n为树的节点总个数,问有多少种可以使所有同层节点的子节点数目相同的树的方案。
解题思路:由于是输入一个数,得到另一个数,所以当然要愉快地打表。而打表怎么打呢。这道题用递归做。循环根结点的子节点个数,如果所给节点数可以完美的分给某个子节点数的方案,就把这种方案加到答案里。由于每一层都是同一类型的问题,所以就相当于一种dp,把子节点的方案数再加到根节点的方案数中,打表就完成了。注意,题中要求对10^9+7取余,不要忘。

#include <iostream>
#include <cstdio>

using namespace std;
const int maxn = 1000 + 5;
const int mod = 1000000000 + 7;

int main()
{
    int n, kase = 0;
    long long y[maxn] = {0};
    y[1] = 1;
    y[2] = 1;
    y[3] = 2;
    for (int i = 4; i <= 1000; i++) {
        for (int j = 1; j < i; j++) {
            if ((i - 1) % j == 0) {//看看是否能够完美的分出j个子节点
                y[i] += y[(i - 1) / j];//把子节点的方案数加上
                y[i] %= mod;//题中要求取余
            }
        }
    }
    while (scanf ("%d", &n) != EOF) {
        printf ("Case %d: %lld\n", ++kase, y[n]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值