uva10247 - Complete Tree Labeling

本文详细解析了如何通过逆向思维确定深度为d的满k叉树的编号方法数量,深入理解满k叉树的特性及节点编号规律。

Problem B

Complete Tree Labeling!

Input: standard input

Output: standard output

Time Limit: 45 seconds

Memory Limit: 32 MB

 

A complete k-ary tree is a k-ary tree in which all leaves have same depth and all internal nodes have degreek. Thisk is also known as the branching factor of a tree. It is very easy to determine the number of nodes of such a tree. Given the depth and branching factor of such a tree, you will have to determine in how many different ways you can number the nodes of the tree so that the label of each node is less that that of its descendants. You should assume that for numbering a tree withN nodes you have the(1, 2, 3, N-1, N) labels available.

 

Input

The input file will contain several lines of input. Each line will contain two integersk andd. Here k is the branching factor of the completek-ary tree andd is the depth of the complete k-ary tree (k>0, d>0, k*d<=21).

 

Output

For each line of input, produce one line of output containing a round number, which is the number of ways thek-ary tree can be labeled, maintaining the constraints described above.

 

Sample Input:

2 2

10 1

 

Sample Output:

80

3628800


  给一个满k叉数从1到N编号,父亲要小于儿子的编号,问深度为d的满k叉数有多少种编号方法。

  这道题要有逆向思维,用cnt[i][j]表示深度为j的i叉数有多少个结点,dp[i][j]表示深度为j的i叉树可以编号的方法数。一个深度为j的i叉树是由一个根和i个深度为j-1的i叉树组成的,根是一定要编号为1的。那么根下的第1个深度为j-1的i叉树的编号方法数:从cnt[i][j]-1里选cnt[i-1][j]个编号,再乘以dp[i-1][j](因为选的这cnt[i-1][j]个编号互不相同,和编号1-cnt[i-1][j]能构成的深度为j-1的i叉树的排列数相同)。同理根下的第2个深度为j-1的i叉树的编号方法数:从刚挑完剩下的cnt[i][j]-1-cnt[i-1][j]中选cnt[i-1][j]个编号,再乘以dp[i-1][j]。

  设s=cnt[i][j]-1,m=cnt[i-1][j],因此dp[i][j]=C(s,m)*dp[i-1][j]*C(s-m,m)*dp[i-1][j]*C(s-2m,m)*dp[i-1][j].....*C(m,m)*dp[i-1][j]

  cnt[i][j]=cnt[i][j-1]*2+1

  初始化cnt[i][0]=1,dp[i][0]=1。

  cnt不用大数,dp要大数。。写个JAVA真恶心。。方法要用static。。

import java.util.*;
import java.math.*;
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BigInteger [][]dp=new BigInteger[25][25];
		int [][]cnt=new int[25][25];
		int i,j,k;
		for(i=1;i<=21;i++){
			cnt[i][0]=1;
			dp[i][0]=BigInteger.ONE;
		}
		for(i=1;i<=21;i++)
			for(j=1;j<=21;j++) cnt[i][j]=cnt[i][j-1]*i+1;
		for(i=1;i<=21;i++)
			for(j=1;j<=21/i;j++){
				dp[i][j]=BigInteger.ONE;
				int m=cnt[i][j-1],s=cnt[i][j]-1;
				for(k=s;k>=m;k=k-m){
					dp[i][j]=dp[i][j].multiply(C(k,m)).multiply(dp[i][j-1]);
				}
			}
		Scanner read=new Scanner(System.in);
		while(read.hasNext()){
			int K=read.nextInt();
			int D=read.nextInt();
			System.out.println(dp[K][D]);
		}
	}
	static BigInteger C(int a,int b){
		BigInteger ret=BigInteger.ONE;
		int i;
		for(i=a;i>a-b;i--) ret=ret.multiply(BigInteger.valueOf(i));
		for(i=b;i>0;i--) ret=ret.divide(BigInteger.valueOf(i));
		return ret;
	}
}


### 基于分布匹配的主动标注方法 在机器学习领域,基于分布匹配的主动标注(Distribution Matching Based Active Labeling, DMAL)是一种旨在通过最小化源域和目标域之间的差异来提高模型泛化能力的技术。该方法特别适用于跨域适应场景,在这些场景中,训练数据和测试数据可能来自不同的分布。 #### 方法概述 DMAL的核心理念在于选择那些能够使源域和目标域特征空间尽可能相似的样本进行人工标注。具体来说: - **初始阶段**:从大量未标记的目标域数据集中随机选取一部分作为种子集合,并对其进行人工标注。 - **迭代过程**:利用已有的带标签数据训练一个分类器;接着计算剩余无标签数据与当前已有标签数据之间分布的距离度量;最后挑选出使得两个分布最接近的一批样本来请求专家标注[^1]。 #### 实现细节 为了实现上述流程中的关键环节——即如何衡量不同分布间的距离并据此选择最优样本,研究者们提出了多种策略和技术手段: - **最大均值差异(Maximum Mean Discrepancy, MMD)**:这是一种广泛使用的核方法,用来量化两组样本所属概率密度函数间差距大小的一种统计工具。MMD能够在不需要显式估计任何参数的情况下比较任意复杂形式的概率分布[^2]。 - **对抗网络(Adversarial Networks)**:近年来兴起的一个热门方向是采用生成对抗网络(GANs)框架下的判别器部分充当二元分类器的角色,以此评估真假样本混合体内部结构特性上的异同程度。这种方法不仅限定了理论边界而且提供了更加灵活有效的解决方案[^3]。 ```python import numpy as np from sklearn.metrics import pairwise_distances_argmin_min def distribution_matching_active_labeling(X_unlabeled, X_labeled): """ Select samples from unlabeled set that minimize the distance between distributions. Args: X_unlabeled (numpy.ndarray): Unlabeled data points. X_labeled (numpy.ndarray): Labeled data points. Returns: list: Indices of selected samples to be labeled next. """ distances = pairwise_distances_argmin_min
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值