Uva 10247 (组合计数)

来源:互联网 发布:达内和尚观linux哪个好 编辑:程序博客网 时间:2024/05/29 16:55

题目:

有一颗 K 叉完全树, 深度 D,有 N 个节点, 给你 [1 - N] 给这颗树的节点标号。
父节点 要比所有子节点的标号小。 问有多少种标号方法。

分析:

题目的标号是不允许重复的。所以根节点的表示是最小的。
对于一颗 K 叉 D 深度的树, 其节点数为 Nk,d, 首先根节点的标号数是最小的。
然后就会剩下 N-1 个标号, 就相当于把 N-1个数分为 K 组, 每组 Nk,d1
对于这些分配方案就有 T=(Nk,d1kNk,d1Nk,d1);
每颗子树的标号方案是互不影响的, 所以有答案: Ansk,d=T(Ansk,d1)k

Code:

import java.math.*;import java.util.*;public class Main {    public static int[][] N = new int[25][25];    public static BigInteger[][] Ans = new BigInteger[25][25];    public static BigInteger C(int m, int n) {        if(n > m) return BigInteger.ZERO;        if(n > (m-n)) return C(m,m-n);        BigInteger ans = BigInteger.ONE;        //System.out.println(n);        for(int i = 0; i < n; ++i) {            ans = ans.multiply(BigInteger.valueOf((long)(m-i))).divide(BigInteger.valueOf((long)(i+1)));        }        return ans;    }    public static void INIT() {        for(int i = 1; i < 25; ++i) {            int tmp = 1;            N[i][0] = 1;            for(int j = 1; j < 25; ++j) {                tmp = tmp * i;                N[i][j] = N[i][j-1] + tmp;            }        }    }    public static void GetAns() {        for(int i = 1; i < 25; ++i) {            Ans[i][0] = BigInteger.ONE;            for(int j = 1; i*j < 25; ++j) {                Ans[i][j] = BigInteger.ONE;                int n = N[i][j-1];                int m = N[i][j];                for(int k = 0; k < i; ++k) {                    Ans[i][j] = Ans[i][j].multiply(Ans[i][j-1]);                    if(m-1-k*n >= n) {                        Ans[i][j] = Ans[i][j].multiply(C(m-1-k*n, n));                    }                    //System.out.println(Ans[i][j]);                }            }        }    }    public static void main(String[] args) {        INIT();        GetAns();        Scanner cin = new Scanner(System.in);        while(cin.hasNext()) {            System.out.println(Ans[cin.nextInt()][cin.nextInt()]);            //System.out.println(C(cin.nextInt(), cin.nextInt()));        }    }}
0 0
原创粉丝点击