UVa 10247 Complete Tree Labeling: Combinatorial Mathematics & High Precision

Source: Internet
Author: User
Tags pow time limit


10247-complete Tree Labeling



Time limit:15.000 seconds



Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=1188



A complete k-ary tree are a k-ary tree in which all leaves have same depth and all internal nodes have K. This k is also known as the branching factor of a. It is very easy to determine the number of nodes of such a. Given the depth and branching factor of such a tree, you'll have to determine into how many different ways you can The nodes of the "tree" so this is the label of the "Each node is less" that "that" of its descendants. You should assume this for numbering a and N nodes you have the (1, 2, 3, N-1, N) labels available.



Input



The input file would contain several lines of input. Each line would contain two integers k and d. Here k is the branching factor of the complete k-arytree and D are the depth of the complete k-ary tree (k>0, d>0, k* D<=21).



Output



For the/line of input, produce one line of output containing a round number, which is the number of ways "k-ary tree C" An is labeled, maintaining the constraints described above.



Sample Input:



2 2



10 1



Sample Output:



80



3628800



Ideas:



Use Ans[i][j] to denote the number of numbered schemes of a full I fork tree with a depth of J, with Node[i][j] representing the knot points of a full I fork tree with a depth of J. First the root node must be the smallest number, and then there are node[i][j]-1 number available, we want to assign them to the I Shang tree, each Shang tree get m=node[i][j-1] number. Use C[n][k] to represent the number of combinations, so a total of c[n[i][j]-1][m]*c[n[i][j]-1-m][m]*c[n[i][j]-1-2*m][m]*...*c[m][m] is assigned. Continue to label the subtree, a total of ans[i][j-1]^i. So get the recursive type:



This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45357.htm



ans[i][j]=c[n[i][j]-1][m]*c[n[i][j]-1-m][m]*c[n[i][j]-1-2*m][m]*...*c[m][m]* (Ans[i][j-1]^i).



High precision Note: When k=5,d=4, the result is 1774 bits.



Complete code:


/*0.549s*/
    
import java.util. *;
import java.io. *;
import java.math. *;
    
public class Main {
    static Scanner cin = new Scanner (new BufferedInputStream (System.in));
    static final int N = 22;
    
    static int [] [] node = new int [N] [N];
    static BigInteger [] [] ans = new BigInteger [N] [N];
    
    static BigInteger c (int n, int k) {// calculate the number of combinations
        if (k> n)
            return BigInteger.ZERO;
        if (k> n-k)
            return c (n, n-k);
        BigInteger ans = BigInteger.ONE;
        for (int i = 0; i <k; ++ i)
            ans = ans.multiply (BigInteger.valueOf (n-i)). divide (BigInteger.valueOf (i + 1));
        return ans;
    }
    
    static void init () {
        for (int i = 1; i <N; ++ i) {
            int pow = 1;
            node [i] [0] = 1;
            for (int j = 1; j <N; ++ j) {
                pow * = i;
                node [i] [j] = node [i] [j-1] + pow; // compute the number of nodes in a full i-ary tree with depth j
            }
        }
        for (int i = 1; i <N; ++ i) {
            ans [i] [0] = BigInteger.ONE;
            for (int j = 1; i * j <N; ++ j) {
                ans [i] [j] = BigInteger.ONE;
                int m = node [i] [j-1];
                for (int k = 0; k <i; ++ k)
                    ans [i] [j] = ans [i] [j] .multiply (ans [i] [j-1]). multiply (c (node [i] [j]-1-k * m, m)) ;// Calculation results  
            }
        }
    }
    
    public static void main (String [] arg) {
        init ();
        while (cin.hasNextInt ())
            System.out.println (ans [cin.nextInt ()] [cin.nextInt ()]);
    }
} 

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.