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的更多相关文章

  1. ZOJ Problem Set - 3822Domination(DP)

    ZOJ Problem Set - 3822Domination(DP) problemCode=3822">题目链接 题目大意: 给你一个n * m的棋盘,每天都在棋盘上面放一颗棋子 ...

  2. zoj 3537 Cake 区间DP (好题)

    题意:切一个凸边行,如果不是凸包直接输出.然后输出最小代价的切割费用,把凸包都切割成三角形. 先判断是否是凸包,然后用三角形优化. dp[i][j]=min(dp[i][j],dp[i][k]+dp[ ...

  3. ZOJ 3623 Battle Ships DP

    B - Battle Ships Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Subm ...

  4. zoj 2860 四边形优化dp

    Breaking Strings Time Limit: 2 Seconds        Memory Limit: 65536 KB A certain string-processing lan ...

  5. 【Codeforces629C】Famil Door and Brackets [DP]

    Famil Door and Brackets Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample Inp ...

  6. 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 ...

  7. Problem Arrangement ZOJ - 3777(状压dp + 期望)

    ZOJ - 3777 就是一个入门状压dp期望 dp[i][j] 当前状态为i,分数为j时的情况数然后看代码 有注释 #include <iostream> #include <cs ...

  8. 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 ...

  9. ZOJ 3306 状压dp

    转自:http://blog.csdn.net/a497406594/article/details/38442893 Kill the Monsters Time Limit: 7 Seconds ...

随机推荐

  1. Spring的安全机制

    Spring Security:它提供全面的安全性解决方案,同时在Web请求和方法调用处理身份确认和授权,利用依赖注入和aop技术.主要名词: 1,安全拦截器:相当应用的一把锁,能够阻止对应用程序中保 ...

  2. 【BZOJ 2802】 2802: [Poi2012]Warehouse Store (贪心)

    2802: [Poi2012]Warehouse Store Description 有一家专卖一种商品的店,考虑连续的n天.第i天上午会进货Ai件商品,中午的时候会有顾客需要购买Bi件商品,可以选择 ...

  3. CodeForces - 875D High Cry

    题面在这里! 直接考虑每个位置成为最右边的最大值的位置,统计不合法区间,补集转化一下就好啦. 复杂度O(N * 30) #include<bits/stdc++.h> #define ll ...

  4. 【推导】【分类讨论】Codeforces Round #431 (Div. 1) B. Rooter's Song

    给你一个这样的图,那些点是舞者,他们每个人会在原地待ti时间之后,以每秒1m的速度向前移动,到边界以后停止.只不过有时候会碰撞,碰撞之后的转向是这样哒: 让你输出每个人的停止位置坐标. ①将x轴上初始 ...

  5. 【后缀自动机】poj1509 Glass Beads

    字符串最小表示 后缀自动机 O(n) 把串复制一次,链接在后面之后,建立SAM,贪心地在SAM上转移,每次贪心地选择最小的字符,转移的长度为n时停止. 输出时由于要最靠前的,所以要在endpos集合中 ...

  6. Android程序设计

    Android程序设计-1 要求安装 Android Stuidio 完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号,提交代码运行截图和码云Git链 ...

  7. Inno Setup入门(十九)——Inno Setup类参考(5)

    单选按钮 单选按钮在安装中也很常见,例如同一个程序可以选择安装不同的性质的功能,例如选择32位或者64位等,两者是排他性的,因此可以通过单选按钮(RadioButton)来实现,在同一个容器中放置的单 ...

  8. [转]SQL Server 2008支持将数据导出为脚本

    本文转自:http://blog.csdn.net/studyzy/article/details/4303759 以前我们要将一个表中的数据导出为脚本,那么只有在网上找一个导出数据的Script,然 ...

  9. [Todo] C++学习资料进度

    <C++必知必会> /Users/baidu/Documents/Data/Interview/C++

  10. xss编码小结

    一.JS编码与HTML编码区分: HTML实体可以使用十进制与十六进制编码:javascript可以使用Unicode与八进制与十六进制进行编码. 二.编码原理区分: 三.编码与非编码 对于JS编码: ...