U E D R , A S I H C RSS

Complete Tree Labeling/하기웅


( )

트 트 = (^(+1) - 1)/(-1) .
1)) 2 2 트 트 = (2^(2+1) - 1)/(2-1) = 7

.
2)) 1 7 트 1 3 2 3 3 .
combination( , /) .
combination(6, 6/2) 2! .
1 = ! * combination( , /)
2 1 트 트 .

~cpp
#include <iostream>
#include <cmath>
#include "BigInteger.h"
using BigMath::BigInteger;

int depth, level, nodeNum, temp, templevel, tempdepth, select, i;

BigInteger labelingNum;

BigInteger factorial[3300];

void InitFactorial()
{
	factorial[1] = 1;
	for(i=2; i<3300; i++)
		factorial[i] = factorial[i-1] * i;
}

BigInteger combination(int a, int b)
{
	if(a==b)
		return 1;
	return factorial[a]/(factorial[b]*factorial[a-b]);
}

BigInteger getCompleteTreeLabeling(int l, int d)
{
	labelingNum=1;
	tempdepth=1;
	if(l==1)
		return 1;
	nodeNum = (pow(l, d+1)-1)/(l-1);
	nodeNum--;
	while(true)
	{
		if(nodeNum==0)
			return labelingNum/l;
		select = nodeNum/l;
		labelingNum = labelingNum * factorial[l] * combination(nodeNum, select);
		nodeNum = select-1;
		tempdepth++;
	}
}

int main()
{
	InitFactorial();
	while(cin>>level>>depth)
		cout << getCompleteTreeLabeling(level, depth) << endl;
	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:58
Processing time 0.0114 sec