UVA - 1645 Count (统计有根树)(dp)

题意:输入n(n <=1000),统计有多少个n结点的有根树,使得每个深度中所有结点的子结点数相同。输出数目除以109+7的余数。

分析:

1、dp[i],i个结点的有根树个数

2、假设n=7,则根结点之外有6个结点。

根的子树有四种情况:

(1)6个结点数为1的子树

(2)3个结点数为2的子树

(3)2个结点数为3的子树

(4)1个结点数为6的子树

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b) {
    if(fabs(a - b) < eps)  return 0;
    return a < b ? -1 : 1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int dp[MAXN];
void init(){
    dp[1] = 1;
    for(int i = 2; i <= 1000; ++i){
        for(int j = 1; j < i; ++j){
            if((i - 1) % j == 0){//i-1除去根结点后的结点数,j为子树的结点数
                (dp[i] += dp[j]) %= MOD;
            }
        }
    }
}
int main(){
    int kase = 0;
    int n;
    init();
    while(scanf("%d", &n) == 1){
        printf("Case %d: %d\n", ++kase, dp[n]);
    }
    return 0;
}

  

posted @ 2017-02-12 16:18  Somnuspoppy  阅读(434)  评论(0编辑  收藏  举报