HDU 1294 Rooted Trees Problem

时间:2022-11-04 08:57:26

题目大意:求有n个节点的树有几种?

题解:http://www.cnblogs.com/keam37/p/3639294.html

#include <iostream>
typedef long long LL;
using namespace std;
LL f[41];
int cnt[41],n;
LL C(LL n,LL m){
m=m<(n-m)?m:(n-m);
LL ans=1;
for(int i=1;i<=m;i++)ans=ans*(n-i+1)/i;
return ans;
}
int dfs(int temp,int left){
if(left==0){
LL ans=1;
for(int i=1;i<=n;i++){
if(cnt[i]==0)continue;
ans=ans*C(f[i]+cnt[i]-1,cnt[i]);
}
f[n]+=ans; return 0;
}
for(int i=temp;i<=left;i++)cnt[i]++,dfs(i,left-i),cnt[i]--;
return 0;
}
int main(){
f[1]=f[2]=1;
for(n=3;n<=40;n++)dfs(1,n-1);
while(cin>>n)cout<<f[n]<<endl;
return 0;
}