uva 10157 expressions
来源:互联网 发布:sql数据库安全性 编辑:程序博客网 时间:2024/06/08 17:06
Problem A: 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.
()(())()
(()(()))
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 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:
(())()
()(())
(()())
#include <cstdio>#include <cstring>#include <cassert>const int LEN = 100;const int NMAX = 300;const int DMAX = 150;int f[NMAX + 1][DMAX + 1][LEN];bool isCalculated[NMAX + 1][DMAX + 1];void add (int a[], int b[]){ int carry = 0; for (int i=0; i<LEN; ++i) { int n = (carry + a[i] + b[i]) % 10; carry = (carry + a[i] + b[i]) / 10; a[i] = n; } assert (carry == 0);}void substract (int a[], int b[]){ int borrow = 0; for (int i=0; i<LEN; ++i) { int n = (a[i] - b[i] + borrow + 10) % 10; borrow = (a[i] - b[i] + borrow + 10) / 10 - 1; a[i] = n; } assert (borrow == 0);}void multiply (int a[], int b[]){ static int c[LEN]; memset (c, 0, sizeof (c)); int I = LEN; while (--I >= 0 && a[I] == 0) {} ++I; int K = LEN; while (--K >= 0 && b[K] == 0) {} ++K; for (int i=0; i<I; ++i) { for (int k=0; k<K && i+k<LEN; ++k) { c[i + k] += a[i] * b[k]; } } int carry = 0; for (int i=0; i<LEN; ++i) { int n = (carry + c[i]) % 10; carry = (carry + c[i]) / 10; c[i] = n; } assert (carry == 0); memcpy (a, c, sizeof (c));}void print (int a[]){ int i = LEN; while (--i > 0 && a[i] == 0) {} while (i >= 0) { printf ("%d", a[i--]); } printf ("\n");}void calculate (int n, int d){#ifndef ONLINE_JUDGE printf ("calculate (%d, %d)\n", n, d);#endif if (isCalculated[n][d]) { return; } isCalculated[n][d] = true; for (int i=0; i<LEN; ++i) { f[n][d][i] = 0; } if (n % 2 == 1) { return; } if (n == 0) { f[n][d][0] = 1; return; } if (d == 0) { return; } static int t[LEN]; for (int i=0; i<n; i+=2) { assert (n - 2 - i >= 0); assert (d - 1 >= 0); calculate (i, d); calculate (n - 2 - i, d - 1); memcpy (t, f[i][d], sizeof (t)); multiply (t, f[n - 2 - i][d - 1]); add (f[n][d], t); }#ifndef ONLINE_JUDGE printf ("f[%d][%d] = ", n, d); print (f[n][d]);#endif}int main (int argc, char *argv[]){ int n, d; while (scanf ("%d%d", &n, &d) == 2) { if (d == 0 && n == 0) { printf ("1\n"); continue; } else if (d == 0) { printf ("0\n"); continue; } calculate (n, d); calculate (n, d - 1); static int ans[LEN]; memcpy (ans, f[n][d], sizeof (ans)); substract (ans, f[n][d - 1]); print (ans); } return 0;}
- uva 10157 expressions
- UVA 10157 Expressions
- uva 10157 - Expressions(dp)
- UVa 10157 - Expressions
- uva 10157Expressions
- UVa Problem Solution: 10157 - Expressions
- Expressions uva
- UVa Problem 10157 Expressions (括号表达式)
- UVa 10157 Expressions (组合数学&高精度)
- UVa:10157 Expressions(动态规划+大数)
- UVA题目10157 - Expressions(DP+高精度)
- UVa 11234 Expressions
- uva 11234 - Expressions
- UVa 11234 Expressions
- uva 11234 - Expressions
- UVa 11234 - Expressions
- UVA 11234 - Expressions
- UVa 11234 - Expressions
- ubuntu内核源码树的建立
- grid组件的详解
- python对时间日期做格式化
- JSON-framework 与 JSONKit的初步评估
- Python模块学习 ---- struct 数据格式转换
- uva 10157 expressions
- 生成表结构
- Start Menu Organizer-对开始菜单中的程序进行分类和整理
- 第一篇博文----我也来写写博客
- 解决: ubuntu登录后无法进入桌面
- 多目录Makefile(库及分层目录)
- MyEclipse 安装 使用 Python
- 移位寄存器模型.v
- delphi excel 单元格合并