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.0095 sec