uva 1645 count

本文介绍了一种计算特定条件下等高有根树数量的方法,并给出了一段C++代码实现。通过递推关系,计算出n个节点的等高有根树的数量,并输出结果对1e9+7取模后的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

有多少个n个节点的有根树,满足每层节点的子节点个数相同,输出该数目除以1e9+7的余数。

分析:

这种题目就属于那种,看起来很高冷,读完题更高冷。

言归正传,根据题意,这棵树是关于根节点对称的,对称性非常好,根节点下面的子树也完全相同。

所以就有了如下递推关系:

//  Created by Chenhongwei in 2015.
//  Copyright (c) 2015 Chenhongwei. All rights reserved.

#include "iostream"
#include "cstdio"
#include "cstdlib"
#include "cstring"
#include "climits"
#include "queue"
#include "cmath"
#include "map"
#include "set"
#include "stack"
#include "vector"
#include "sstream"
#include "algorithm"
using namespace std;
const int inf=1e8;
const int maxn=1e5;
const int mod=1e9+7;
typedef long long ll;
int dp[1100];
int main()
{	
	//ios::sync_with_stdio(false);
	// freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	memset(dp,0,sizeof dp);
	dp[1]=dp[2]=1;
	dp[3]=2,dp[4]=3;
	for(int i=5;i<=1005;i++)
		for(int j=1;j<=i-1;j++)
			if((i-1)%j==0)
				dp[i]+=dp[j],dp[i]%=mod;
	int n,kase=0;
	while(~scanf("%d",&n))
		printf("Case %d: %d\n",++kase,dp[n]);
	
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值