A complete k-ary tree is a k-ary tree in which all leaves have same depth and all internal nodes have degree k. This k 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 with N 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 integers k and d. Here k is the branching factor of the complete k-ary tree and d 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 the k-ary tree can be labeled, maintaining the constraints described above.
Sample Input
2 2
10 1
Sample Output
80
3628800
问题链接:UVA10247 Complete Tree Labeling
问题简述:(略)
问题分析:大数+递推问题,不解释。
程序说明:用Java语言程序来解决。
参考链接:(略)
题记:(略)
AC的Java语言程序如下:
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static BigInteger C(int m, int n)
{
if (m - n < n) n = m - n;
BigInteger res = BigInteger.ONE;
for (int i = 1; i <= n; i ++)
res = res.multiply(BigInteger.valueOf(m - i + 1)).divide(BigInteger.valueOf(i));
return res;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int N = 21;
BigInteger[][] f = new BigInteger[N + 1][N + 1];
int[][] g = new int[N + 1][N + 1];
for (int i = 1; i <= N; i ++) {
g[i][0] = 1;
for(int j = 1; i * j <= N; j ++) {
g[i][j] = i * g[i][j - 1];
g[i][j] ++;
}
}
for (int i = 1; i <= N; i ++) {
f[i][0] = BigInteger.ONE;
for(int j = 1; i * j <= N; j ++) {
f[i][j] = BigInteger.ONE;
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]);
}
}
}



