hdu 1234 Rooted Trees Problem 有重复的组合+乘法原理

Rooted Trees Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 443    Accepted Submission(s): 147


Problem Description
Give you two definitions tree and rooted tree. An undirected connected graph without cycles is called a tree. A tree is called rooted if it has a distinguished vertex r called the root. Your task is to make a program to calculate the number of rooted trees with n vertices denoted as Tn. The case n=5 is shown in Fig. 1.
 

Input
There are multiple cases in this problem and ended by the EOF. In each case, there is only one integer means n(1<=n<=40) .
 

Output
For each test case, there is only one integer means Tn.
 

Sample Input
  
  
1 2 5
 

Sample Output
  
  
1 1 9
 

Author
SmallBeer(CML)
 

Source
 

Recommend
lcy

枚举子树的组成,然后计算方法数。
#include<cstdio>
#include<iostream>
using namespace std;
long long t[50];
int cnt[50] , n;

long long gcd(long long a, long long b)
{
    return b?gcd(b,a%b):a;
}

long long C(long long n, long long m)
{
    if(m > n - m ) m = n - m;
    long long mul = 1 , div = 1;
    for(int i = 0; i < m; i++)
    {
        mul *= (n-i);
        div *= (i+1);
        long long g = gcd(mul,div);
        mul/=g , div /= g;
    }
    return mul / div;
}

void dfs(int start, int left)
{
    if( left == 0 )
    {
        long long ans = 1;
        for(int i = 1; i<=n ; i ++)
        {
            if(cnt[i] == 0)continue;
            ans = ans * C(t[i] - 1 + cnt[i] , cnt[i]);
        }
        t[n] += ans;
        return ;
    }
    if(start > left )return;
    cnt[start]++;
    dfs(start , left - start);
    cnt[start]--;
    dfs(start+1 , left);
}

int main()
{

    t[1] = t[2] = 1;
    for(n = 3; n <= 40 ; n++)
    {
        dfs(1,n-1);
    }
    while( ~scanf("%d",&n) )
        printf("%I64d\n",t[n]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值