UVa 10157 Expressions:组合数学&高精度

10157 - Expressions

Time limit: 10.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=34&page=show_problem&problem=1098

Let X be the set of correctly built parenthesis expressions. The elements of X are strings consisting only of the characters "("and ")". The set X is defined as follows:

an empty string belongs to X

if A belongs to X, then (A) belongs to X

if both A and B belong to X, then the concatenation AB belongs to X.

For example, the following strings are correctly built parenthesis expressions (and therefore belong to the set X):

()(())()

(()(()))

The expressions below are not correctly built parenthesis expressions (and are thus not in X):

(()))(()

())(()

Let E be a correctly built parenthesis expression (therefore E is a string belonging to X).

The length of E is the number of single parenthesis (characters) in E.

The depth D(E) of E is defined as follows:

              ì 0 if E is empty

D(E)= í D(A)+1 if E = (A), and A is in X

              max(D(A),D(B)) if E = AB, and A, B are in X

For example, the length of "()(())()" is 8, and its depth is 2.

What is the number of correctly built parenthesis expressions of length n and depth d, for given positive integers n and d?

Task

Write a program which

reads two integers n and d

computes the number of correctly built parenthesis expressions of length n and depth d;

Input data

Input consists of lines of pairs of two integers - n and d, at most one pair on line, 2<= n<= 300, 1<= d<= 150.

The number of lines in the input file is at most 20, the input may contain empty lines, which you don't need to consider.

Output data

For every pair of integers in the input write single integer on one line - the number of correctly built parenthesis expressions of length n and depth d.

Example

Input data                                   Output data

6 2                 3

300 150             1

 

There are exactly three correctly built parenthesis expressions of length 6 and depth 2:

(())()

()(())

(()())

思路:

本文URL地址:http://www.bianceng.cn/Programming/sjjg/201410/45358.htm

我们可以把最左边的“(”和其配对的“)”看成一组分界线,它们把剩余的括号分成了左右两部分,其中左边的深度最多为d-1,右边部分的深度最多为d。

接下来枚举左右两边的括号数,把每种情况累加即可。

使用递归,设f[n][d]表示一共有n对括号时深度不超过d的表达式的数量,那么f(n,d) = sum{f(i, d - 1)*f(n - i - 1, d)} (0<=i<n),最后输出的结果即是f(n/2,d)-f(n/2,d-1)。

注意:题目保证了输入的括号串是合法的(n保证是偶数)。

完整代码:

/*0.752s*/

import java.io.*;
import java.util.*;
import java.math.*;  

public class Main {
    static Scanner cin = new Scanner(new BufferedInputStream(System.in));
    static BigInteger[][] f = new BigInteger[151][151];
    static boolean[][] vis = new boolean[151][151];  

    static BigInteger DP(int n, int d) {
        if (vis[n][d])
            return f[n][d];
        vis[n][d] = true;
        if (n <= 1 || d <= 1)
            return f[n][d] = BigInteger.ONE;
        f[n][d] = BigInteger.ZERO;
        for (int i = 0; i < n; i++)
            f[n][d] = f[n][d].add(DP(i, d - 1).multiply(DP(n - i - 1, d)));
        return f[n][d];
    }  

    public static void main(String[] args) {
        for (int i = 0; i < 151; i++) {
            f[i][0] = f[i][1] = f[0][i] = f[1][i] = BigInteger.ONE;
            for (int j = 0; j < 151; j++)
                vis[i][j] = false;
        }
        while (cin.hasNext()) {
            int n = cin.nextInt(), d = cin.nextInt();
            if (n <= 2 || d <= 1) {
                System.out.println("1");
                continue;
            }
            n >>= 1;
            DP(n, d);
            DP(n, d - 1);
            System.out.println(f[n][d].subtract(f[n][d - 1]));
        }
    }
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索length
, and
, of
The
乐高10157、10157、,以便于您获取更多的相关知识。

时间: 2024-10-03 13:46:14

UVa 10157 Expressions:组合数学&amp;高精度的相关文章

UVa 10247 Complete Tree Labeling:组合数学&amp;amp;高精度

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 is a k-ary tree in which all leaves have same d

UVa 10198 Counting:组合数学&amp;amp;高精度

10198 - Counting Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1139 The Problem Gustavo knows how to count, but he is now learning how write numbers. As

UVa 11234 Expressions:二叉树 层次遍历 广搜

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=2175 题目类型: 数据结构, 二叉树 题目大意: 一般情况下,都是中缀操作符, 如x+y.然后题目给出了一种后缀操作符的形式, 变成 x y +. 进行后缀操作可以用栈模拟,使用push,pop, 过程和经典的"括号匹配"差不

UVa 11234 Expressions:二叉树重建&amp;amp;由叶往根的层次遍历

11234 - Expressions Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2175 Arithmetic expressions are usually written with the operators in between the two operands (which

UVa 11609 Teams (组合数学)

11609 - Teams Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=26 56 In a galaxy far far away there is an ancient game played among the planets. The spec

UVa 324 Factorial Frequencies:高精度

324 - Factorial Frequencies Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=260 In an attempt to bolster her sagging palm-reading business, Madam Phoenix

UVa 11375 Matches:DP&amp;amp;高精度

11375 - Matches Time limit: 2.000 seconds http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2370 We can make digits with matches as shown below: Given N matches, find the number of different numbers representable us

UVa 10023 Square root:高精度及开平方公式

10023 - Square root Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=964 The Problem You are to determinate X by given Y, from expression The Input The first line is the n

uva 11234 - Expressions

点击打开链接 题目意思:给定一个字符串表示是一个表达式,要求使用队列代替栈的方法来输出结果. 解题思路:我们知道对于这个表达式,最后一个肯定是大写字母,对于每一个大写字母会跟着两个小写的字母或大写字母,那我们联想到二叉树,我们可以建立一颗二叉表达式的树,怎么建呢,一下代码详解,最后我们只要从最后一层从左往右这样输出即可,要求用到树的层次遍历(就是广搜),同时需要用一个list来存储节点的值,最后输出list即可 代码: #include <cstdio> #include <cstrin