# UVA题目10157 - Expressions（DP+高精度）

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
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?
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

ac代码

 16453524 10157 Expressions Accepted C++ 4.216 2015-11-18 07:47:56
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int M=155;
const int N=105;
struct bign {
int len, sex;
int s[N];

bign() {
this -> len = 1;
this -> sex = 0;
memset(s, 0, sizeof(s));
}

bign operator = (const char *number) {
int begin = 0;
len = 0;
sex = 1;
if (number[begin] == '-') {
sex = -1;
begin++;
}
else if (number[begin] == '+')
begin++;

for (int j = begin; number[j]; j++)
s[len++] = number[j] - '0';
}

bign operator = (int number) {
char string[N];
sprintf(string, "%d", number);
*this = string;
return *this;
}

bign (int number) {*this = number;}
bign (const char* number) {*this = number;}

bign change(bign cur) {
bign now;
now = cur;
for (int i = 0; i < cur.len; i++)
now.s[i] = cur.s[cur.len - i - 1];
return now;
}

void delZore() {    // 删除前导0.
bign now = change(*this);
while (now.s[now.len - 1] == 0 && now.len > 1) {
now.len--;
}
*this = change(now);
}

void put() {    // 输出数值。
delZore();
if (sex < 0 && (len != 1 || s[0] != 0))
cout << "-";
for (int i = 0; i < len; i++)
cout << s[i];
}

bign operator + (const bign &cur){
bign sum, a, b;
sum.len = 0;
a = a.change(*this);
b = b.change(cur);

for (int i = 0, g = 0; g || i < a.len || i < b.len; i++){
int x = g;
if (i < a.len) x += a.s[i];
if (i < b.len) x += b.s[i];
sum.s[sum.len++] = x % 10;
g = x / 10;
}
return sum.change(sum);
}

bign operator * (const bign &cur){
bign sum, a, b;
sum.len = 0;
a = a.change(*this);
b = b.change(cur);

for (int i = 0; i < a.len; i++){
int g = 0;

for (int j = 0; j < b.len; j++){
int x = a.s[i] * b.s[j] + g + sum.s[i + j];
sum.s[i + j] = x % 10;
g = x / 10;
}
sum.len = i + b.len;

while (g){
sum.s[sum.len++] = g % 10;
g = g / 10;
}
}
return sum.change(sum);
}

bign operator - (const bign &cur) {
bign sum, a, b;
sum.len = len;
a = a.change(*this);
b = b.change(cur);

for (int i = 0; i < b.len; i++) {
sum.s[i] = a.s[i] - b.s[i] + sum.s[i];
if (sum.s[i] < 0) {
sum.s[i] += 10;
sum.s[i + 1]--;
}
}
for (int i = b.len; i < a.len; i++) {
sum.s[i] += a.s[i];
if (sum.s[i] < 0) {
sum.s[i] += 10;
sum.s[i + 1]--;
}
}
return sum.change(sum);
}
};
bign dp[M][M];
void DP()
{
int i,j,k;
for(i=0;i<=150;i++)
dp[0][i]=1;
for(i=1;i<=150;i++)
{
for(j=1;j<=150;j++)
{
for(k=0;k<i;k++)
{
dp[i][j]=dp[k][j-1]*dp[i-k-1][j]+dp[i][j];
}
}
}
}
int main()
{
int n,m;
DP();
while(scanf("%d%d",&n,&m)!=EOF)
{
bign sum=dp[n/2][m]-dp[n/2][m-1];
sum.put();
printf("\n");
}
}


• 本文已收录于以下专栏：

## UVa 10157 Expressions (组合数学&高精度)

10157 - Expressions Time limit: 10.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlin...
• synapse7
• 2013年10月18日 12:35
• 1491

## UVA 10157 Expressions

• u011345136
• 2013年11月02日 19:10
• 636

## UVA 10157 - Expressions（dp卡特兰数高精度）

Problem A: EXPRESSIONS  Let X be the set of correctly built parenthesis expressions. The elem...
• u011217342
• 2013年11月20日 22:58
• 1035

## uva 10157 - Expressions(dp)

• u011328934
• 2013年11月06日 17:03
• 1091

## UVa 10157 - Expressions

• mobius_strip
• 2014年09月14日 14:16
• 854

## uva 10157 expressions

Problem A: EXPRESSIONS  Let X be the set of correctly built parenthesis expressions. The el...
• chengouxuan
• 2012年01月17日 20:11
• 363

## uva 10157 Expressions

uva 10157 Expressions
• tengfei461807914
• 2017年08月03日 16:13
• 98

## UVa Problem Solution: 10157 - Expressions

Let n denotes the length of the expression, d denotes the depth at most, and the count is f(n,d). Ob...
• liukaipeng
• 2008年12月03日 01:20
• 2552

## UVa:10157 Expressions(动态规划+大数)

• kkkwjx
• 2014年03月18日 22:24
• 508

## Uva 10157 - Expressions 解题报告（递推）

Problem A: EXPRESSIONS  Let X be the set of correctly built parenthesis expressions. The elem...
• kbdwo
• 2014年04月02日 09:39
• 714

举报原因： 您举报文章：UVA题目10157 - Expressions（DP+高精度） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)