问题
分析
每个深度中所有节点的子节点个数相同,就是说每个父节点的所有子树都是一样的
首先一个点作为根节点,剩下n-1个点,根节点的子树中的节点有可以有k(1<=k<=n-1)个,只要保证(n-1)%k==0,然后转移方程就能够写出来了
d
p
[
n
]
=
∑
k
d
p
[
k
]
,
(
n
−
1
)
%
k
=
0
dp[n]=\sum_kdp[k],(n-1)\%k=0
dp[n]=∑kdp[k],(n−1)%k=0 dp[n]是n个结点组成的满足要求的有根树数量
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn=1005,Inf=0x3f3f3f3f,mod=1000000000+7;
int dp[maxn],n,kase=0;
int main(void){
dp[1]=1;
for(int i=2;i<maxn;++i){
for(int k=1;k<i;++k){
if((i-1)%k==0) {
dp[i] += dp[k];
dp[i] %= mod;
}
}
}
while(scanf("%d",&n)==1){
printf("Case %d: %d\n",++kase,dp[n]);
}
}