UVA_10247

我们不妨先对k叉树由n-1层变成n层时这一情况讨论一下,看看是否能得到一个递推式。

不妨设f[i]表示k叉树n层时的方案种数,设g[i]为k叉树i层时顶点的个数,首先变成n层时,根节点必须填最小的数,根节点的子节点呢?由于标签都是不同的,那么每棵子树都可以从剩下的标签中选出g[n-1]个标签即可。这样我们就得到了递推式f[n]=multiply{C(i*g[n-1],g[n-1])*f[n-1]}(1<=i<=k)。

于是这样我们就可以先把所有结果预处理出来了。

import java.math.BigInteger;import java.util.Scanner;

public class Main {public static void main(String[] args) {        Scanner cin = new Scanner(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;    }}

转载于:https://www.cnblogs.com/staginner/archive/2011/12/17/2290826.html

UVA 10247 Complete Tree Labeling相关推荐

  1. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

  2. 222. Count Complete Tree Nodes

    题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...

  3. leetcode 222 Count Complete Tree Nodes

    今天做了一道leetcode题,刚开始是暴力破解,发现special judge, very large tree 过不去,然后一顿想,一顿查,发现可以运用complete binary tree 的 ...

  4. 222 Count Complete Tree Nodes

    1,这道题如果纯用递归数点而不利用其为一个complete binary tree的话会超时. 2.为了利用这个条件,比较左右两子数的高度:1, 如果相等则左子树为完全二叉树 2, 如果不等, 则右子 ...

  5. Middle-题目95:222. Count Complete Tree Nodes

    题目原文: Given a complete binary tree, count the number of nodes. 题目大意: 给出一个完全二叉树,求节点数. 题目分析: 如果直接递归数节点 ...

  6. Count Complete Tree Nodes

    https://leetcode.com/problems/count-complete-tree-nodes/ 宽度优先搜索方法,超时!! /*** Definition for a binary ...

  7. LeetCode Count Complete Tree Nodes(二分法)

    问题:给出一个完全二叉树,求其结点个数 思路:第一种方式时直接使用递归法,将其左子树个数加上右子树个数再加上根结点 第二种方式二分法,因为完全二叉树除了最后一层外,其它都满足有2^i个结点,而最后一层 ...

  8. leetcode 222. Count Complete Tree Nodes | 222. 完全二叉树的节点个数(Java)

    题目 https://leetcode.com/problems/count-complete-tree-nodes/ 题解 思路参考左程云<程序员代码面试指南> 顺便贴一下草稿 代码 c ...

  9. LeetCode题解-222-Count Complete Tree Nodes

    没有做出来,所以参考了https://segmentfault.com/a/1190000003818177 迭代法的图解如下:

  10. LeetCode 222. Count Complete Tree Nodes 题解——Java

    题目链接:https://leetcode.com/problems/count-complete-tree-nodes/#/description 题目要求:计算完全二叉树的节点个数 思路:首先想到 ...

最新文章

  1. c++ 调用python返回指针
  2. 填坑-十万个为什么?(24)
  3. restful和rest_HATEOAS的RESTful服务:JVM上的REST API和超媒体
  4. 三元运算符(Java)
  5. mac 安装mysql 找不到_mac安装mysql遇到的坑
  6. 哈工大中文分词系统LTP(pyltp)学习笔记
  7. OpenCV+QT5在Window下的环境配置记录
  8. Flink 统计页面点击量
  9. Aborted (core dumped)
  10. 铁流:代码中的汉字,为什么能挡住CIA黑客
  11. 微信小程序:获取用户手机号
  12. 企业增强型融合通讯解决方案Cnskype
  13. xlrd读取Excel数据
  14. 计算机在机械智能制造中的应用,机电一体化技术应用于智能制造中的优势研究...
  15. 参禅静坐--虚极静笃--快速恢复脑力体力
  16. 10w 行级别数据的 Excel 导入,完整优化记录
  17. ReplicaSet、DaemonSet
  18. Android 之路44---四大组件之ContentProvider
  19. Jumping Frog
  20. 揭秘宜信财富年度账单的技术实现

热门文章

  1. SIFT特征匹配算法介绍——寻找图像特征点的原理
  2. Matlab论文插图绘制模板—柱状图(单组多色)
  3. java B2B2C springmvc mybatis仿淘宝电子商城系统-服务消费者(rest+ribbon)
  4. Linux系统CentOS 7中安装配置JDK
  5. [daily][device] linux添加打印机
  6. 创新工场 DeeCamp 2019 人工智能训练营启动报名,计划招收至少 600 名大学生...
  7. PS使用:利用PS去除图片中的多余文字
  8. 报表工具能实现怎么的导出效果?
  9. 数商云钢材行业智慧供应商管理系统:降低企业运营成本,构建数字化供应商管理体系
  10. 计算机运行大型游戏特热,玩大型游戏cpu温度多少度算正常