ZOJ 2604 Little Brackets DP
DP:
- 边界条件:dp[0][j] = 1
- 递推公式:dp[i][j] = sum{dp[i-k][j] * dp[k-1][j-1] | 0<k≤i}
i对括号深度不超过j的,能够唯一表示为(X)Y形式,当中X和Y能够为空,设X有k-1对括号,则相应的方案数为dp[i-k][j] * dp[k-1][j-1]
Little Brackets
Time Limit: 2 Seconds Memory Limit: 65536 KB
Consider all regular bracket sequences with one type of brackets. Let us call the depth of the sequence the maximal difference between the number of opening and the number of closing
brackets in a sequence prefix. For example, the depth of the sequence "()()(())" is 2, and the depth of "((()(())()))" is 4.
Find out the number of regular bracket sequences with n opening brackets that have the depth equal to k. For example, for n = 3 and k = 2 there are three such sequences: "()(())", "(()())",
"(())()".
Input
Input file contains several test cases. Each test case is described with n and k (1 <= k <= n <= 50).
Last testcase is followed by two zeroes. They should not be processed.
Output
For each testcase output the number of regular bracket sequences with n opening brackets that have the depth equal to k.
Separate output for different testcases by a blank line. Adhere to the format of the sample output.
Sample Input
3 2
37 23
0 0
Sample Output
Case 1: 3 Case 2: 203685956218528
Author: Andrew Stankevich
Source: Andrew Stankevich's Contest #7
import java.util.*;
import java.math.*; public class Main
{
static BigInteger dp[][] = new BigInteger[55][55]; static void INIT()
{
for(int i=0;i<55;i++)
for(int j=0;j<55;j++) dp[i][j]=BigInteger.ZERO; for(int i=0;i<55;i++) dp[0][i]=BigInteger.ONE; for(int i=1;i<=50;i++)
{
for(int j=1;j<=50;j++)
{
for(int k=1;k<=i;k++)
{
dp[i][j]=dp[i][j].add(dp[i-k][j].multiply(dp[k-1][j-1]));
}
}
}
} public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
INIT();
int cas=1;
boolean pr = false;
while(in.hasNext())
{
int n=in.nextInt(),k=in.nextInt();
if(n==0&&k==0) break;
if(pr) System.out.println("");
System.out.println("Case "+(cas++)+": "+dp[n][k].subtract(dp[n][k-1]));
pr=true;
}
}
}
ZOJ 2604 Little Brackets DP的更多相关文章
- ZOJ Problem Set - 3822Domination(DP)
ZOJ Problem Set - 3822Domination(DP) problemCode=3822">题目链接 题目大意: 给你一个n * m的棋盘,每天都在棋盘上面放一颗棋子 ...
- zoj 3537 Cake 区间DP (好题)
题意:切一个凸边行,如果不是凸包直接输出.然后输出最小代价的切割费用,把凸包都切割成三角形. 先判断是否是凸包,然后用三角形优化. dp[i][j]=min(dp[i][j],dp[i][k]+dp[ ...
- ZOJ 3623 Battle Ships DP
B - Battle Ships Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- zoj 2860 四边形优化dp
Breaking Strings Time Limit: 2 Seconds Memory Limit: 65536 KB A certain string-processing lan ...
- 【Codeforces629C】Famil Door and Brackets [DP]
Famil Door and Brackets Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample Inp ...
- Codeforces Round #343 (Div. 2) C. Famil Door and Brackets dp
C. Famil Door and Brackets 题目连接: http://www.codeforces.com/contest/629/problem/C Description As Fami ...
- Problem Arrangement ZOJ - 3777(状压dp + 期望)
ZOJ - 3777 就是一个入门状压dp期望 dp[i][j] 当前状态为i,分数为j时的情况数然后看代码 有注释 #include <iostream> #include <cs ...
- Codeforces Round #288 (Div. 2) E. Arthur and Brackets [dp 贪心]
E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input stand ...
- ZOJ 3306 状压dp
转自:http://blog.csdn.net/a497406594/article/details/38442893 Kill the Monsters Time Limit: 7 Seconds ...
随机推荐
- Codeforces Round #260 (Div. 1) Boredom(DP)
Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- Xamarin XAML语言教程模板页面TemplatedPage
Xamarin XAML语言教程模板页面TemplatedPage 模板页面TemplatedPage 在上文中我们提到了TemplatedPage,它被称为模板页面,用来显示控件模版.Templat ...
- [BZOJ4859][BJOI2017]机动训练(DP)
4859: [BeiJing2017]机动训练 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 105 Solved: 63[Submit][Stat ...
- POJ 1740 A New Stone Game 又是博弈论配对找规律orz 博弈论 规律
http://poj.org/problem?id=1740 这个博弈一眼看上去很厉害很高大上让人情不自禁觉得自己不会写,结果又是找规律…… 博弈一般后手胜都比较麻烦,但是主要就是找和先手的对应关系, ...
- Problem D: 程序填充(递归函数):数列2项和
Problem D: 程序填充(递归函数):数列2项和 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 2601 Solved: 2117 Descrip ...
- Linux下Git命令中文显示乱码的问题解决:274\232\350\256\256\346\200\273\347\273\223
使用git add添加要提交的文件的时候,如果文件名是中文,会显示形如274\232\350\256\256\346\200\273\347\273\223的乱码. 解决方案:在bash提示符下输入: ...
- HDU 4643 GSM (2013多校5 1001题 计算几何)
GSM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submiss ...
- 利用tempo将json数据填充到html模板
1.下载tempo 2.使用 <!DOCTYPE html> <html> <head lang="zn-ch"> <meta chars ...
- [Eclipse插件] 安装和使用JD-Eclipse插件
JD-Core 是一个免费的库,从一个或多个“.class”文件中 重构Java源代码.JD-Core可以用来恢复丢失的源代码,并深究Java运行时类库.支持Java 5的功能:如注释,泛型或键入“枚 ...
- Openshift template的使用
1.template的定义 官方对template的定义是 A template describes a set of objects that can be parameterized and pr ...