UVA10157 Expressions大数+递推
Posted 海岛Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10157 Expressions大数+递推相关的知识,希望对你有一定的参考价值。
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:
D
(
E
)
=
0
if E is empty
D
(
A
)
+
1
if E = (A), and A is in X
m
a
x
(
D
(
A
)
,
D
(
B
)
)
if E = AB, and A, B are in X
D(E) = \\left \\ \\beginarraylr 0 & \\text if E is empty\\\\ D(A) + 1 & \\text if E = (A), and A is in X\\\\ max(D(A), D(B)) & \\text if E = AB, and A, B are in X \\endarray \\right.
D(E)=⎩⎨⎧0D(A)+1max(D(A),D(B))if E is emptyif E = (A), and A is in Xif 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?
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
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
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.
Note: There are exactly three correctly built parenthesis expressions of length 6 and depth 2:
(())()
()(())
(()())
Sample Input
6 2
300 150
Sample Output
3
1
问题链接:UVA10157 Expressions
问题简述:(略)
问题分析:大数+递推问题,不解释。
程序说明:用Java语言程序来解决。
参考链接:(略)
题记:(略)
AC的Java语言程序如下:
/* UVA10157 Expressions */
import java.math.BigInteger;
import java.util.Scanner;
public class Main
public static void main(String[] args)
Scanner cin = new Scanner(System.in);
int N = 150;
BigInteger[][] f = new BigInteger[N + 1][N + 1];
for(int i = 0; i <= N; i ++)
f[0][i] = BigInteger.ONE;
for (int i = 1; i <= 150; i ++)
for (int j = 0; j <= 150; j ++)
f[i][j] = BigInteger.ZERO;
for (int i = 1; i <= N; i ++)
for (int j = 1; j <= N; j ++)
for (int k = 0; k < i; k ++)
f[i][j] = f[i][j].add(f[k][j - 1].multiply(f[i - k - 1][j]));
while (cin.hasNext())
int n = cin.nextInt();
int d = cin.nextInt();
if(n % 2 == 1)
System.out.println("0");
else
n /= 2;
System.out.println(f[n][d].add(f[n][d - 1].negate()));
以上是关于UVA10157 Expressions大数+递推的主要内容,如果未能解决你的问题,请参考以下文章
UVA10516 Another Counting Problem大数+递推