栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

UVA10247 Complete Tree Labeling【大数+递推】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

UVA10247 Complete Tree Labeling【大数+递推】

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]);
        }
    }    
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/841423.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号