Ultraviolet A 10247 complete tree labeling

Source: Internet
Author: User

Uva_10247

Let's first discuss this situation when the K-tree is changed from n-1 to n-layer to see if a recursive formula can be obtained.

If f [I] is set to represent the number of solutions for K-tree N layers, and g [I] is set to the number of vertices when K-tree I layers are set to N layers, the minimum number of root nodes is required. What about the child nodes of the root node? Because tags are different, you can select G [n-1] tags from the remaining tags for each subtree. In this way, we obtain the recursive formula F [N] = multiply {C (I * g [n-1], G [n-1]). * f [n-1]} (1 <= I <= K ).

So we can pre-process all the results first.

 Import Java. Math. biginteger;
Import Java. util. vendor;

Public Class Main {
Public Static Void Main (string [] ARGs ){
Scanner CIN = New Using (system. In );
Biginteger [] [] f = New Biginteger [30] [30];
Int [] [] G = New Int [30] [30];
For ( Int I = 1; I <= 21; I ++)
{
F [I] [0] = New Biginteger ("1 ");
G [I] [0] = 1;
For ( Int J = 1; I * j <= 21; j ++)
{
F [I] [J] = New Biginteger ("1 ");
G [I] [J] = I * g [I] [J-1];
G [I] [J] ++;
For ( Int K = I; k> = 1; k --)
F [I] [J] = f [I] [J]. multiply (F [I] [J-1]. multiply (C (K * g [I] [J-1], G [I] [J-1]);
}
}
While (CIN. hasnext ())
{
Int K = cin. nextint ();
Int D = cin. nextint ();
System. Out. println (F [k] [d]);
}
}
Public Static Biginteger C ( Int M,Int N)
{
If (M-n <n)
N = m-N;
Biginteger res = New Biginteger ("1 ");
For ( Int I = 1; I <= N; I ++)
Res = res. Multiply (biginteger. valueof (M-I + 1). Divide (biginteger. valueof (I ));
Return Res;
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.