Count the Trees

本文介绍了一种计算使用特定数量的不同元素可以构建的不同标记二叉树数量的方法。通过递推公式预计算出所需数值,并利用这些数值快速解决输入问题。示例展示了不同元素数量下可构建的二叉树数量。

Count the Trees

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 925 Accepted Submission(s): 608
 
Problem Description
Another common social inability is known as ACM (Abnormally Compulsive Meditation). This psychological disorder is somewhat common among programmers. It can be described as the temporary (although frequent) loss of the faculty of speech when the whole power of the brain is applied to something extremely interesting or challenging. 
Juan is a very gifted programmer, and has a severe case of ACM (he even participated in an ACM world championship a few months ago). Lately, his loved ones are worried about him, because he has found a new exciting problem to exercise his intellectual powers, and he has been speechless for several weeks now. The problem is the determination of the number of different labeled binary trees that can be built using exactly n different elements. 

For example, given one element A, just one binary tree can be formed (using A as the root of the tree). With two elements, A and B, four different binary trees can be created, as shown in the figure. 

If you are able to provide a solution for this problem, Juan will be able to talk again, and his friends and family will be forever grateful. 

 
Input
The input will consist of several input cases, one per line. Each input case will be specified by the number n ( 1 ≤ n ≤ 100 ) of different elements that must be used to form the trees. A number 0 will mark the end of input and is not to be processed. 
 
Output
For each input case print the number of binary trees that can be built using the n elements, followed by a newline character. 
 
Sample Input
1
2
10
25
0
 
Sample Output
1
4
60949324800
75414671852339208296275849248768000000
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        BigInteger[] f = new BigInteger[301];
        BigInteger[] g = new BigInteger[301];
        f[1] = BigInteger.valueOf(1);
        g[1] = BigInteger.valueOf(1);
        for(int i = 2; i <= 300; i++) {
            f[i] = f[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));
            g[i] = g[i-1].multiply(BigInteger.valueOf(i));
        }
        
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()) {
            int n = cin.nextInt();
            if(n == 0)
                break;
            System.out.println(f[n].multiply(g[n]));
        }
    }
}

用中文思考:G. Tree Parking time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input output: standard outputConsider the following problem statement: - You are given a tree with $n$ vertices rooted at $1$. For each $1 \leq i \leq n$, a car will enter the root at time $l_i$. It will then instantaneously travel from the root to vertex $i$ through the unique simple path and park there. It will leave through the same path in the other direction at time $r_i$. During the time when a car is parked in a vertex, it blocks other cars from traveling through that vertex. The tree is called valid if and only if all cars are able to enter and leave the tree at their desired times. Count the number of pairs of sequences $l$, $r$ such that $l_i < r_i$, their concatenation is a permutation of $1 \ldots 2n$, and the tree is valid. Calculate the sum of the answers to the problem over all labeled trees$^{\text{∗}}$ with $n$ vertices and $k$ leaves. The root is not considered a leaf. Since the answer may be large, calculate it modulo $998\,244\,353$. $^{\text{∗}}$Two labeled trees are considered different if and only if their edge sets are different.**Input** Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The only line of each test case contains two integers $n$, $k$ ($1 \leq k < n \leq 2 \cdot 10^5$) — the number of vertices and leaves of the tree, respectively. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.**Output** For each test case, output one integer — the answer modulo $998\,244\,353$.InputCopy 3 2 1 8 3 65 43 OutputCopy 3 899171636 38330886**Note** In the first case, there is only one tree that satisfies the constraints. The correct pairs of sequences are: $$ (l, r) = \bigl([1, 3], [2, 4]\bigr),\:\bigl([3, 1], [4, 2]\bigr),\:\bigl([2, 1], [3, 4]\bigr). $$ C++代码,用中文思考
最新发布
07-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值