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. 洛谷——P1748 H数

    P1748 H数 题目背景 无 题目描述 所谓H数,是指只含有2,3,5,7这些质因数的数,如630是H数,而22不是.现在要求输出第n个H数,为了方便起见将H[1]定为1.已知n不超过10000,最 ...

  2. 第4天:Ansible模块

    Ansible对远程服务器的实际操作实际是通过模块完成的,其工作原理如下: 1)将模块拷贝到远程服务器 2)执行模块定义的操做,完成对服务器的修改 3)在远程服务器中删除模块 需要说明的是,Ansib ...

  3. (转) HA的几种方案

    数据库HA   一般把数据库层面的HA,和应用层面HA分开考虑 数据库一般采用数据库产品提供的HA方案,比如Oracle的RAC,mysql的集群,mongodb的replica set等 无HA的运 ...

  4. SqlHelper——只因为在人群中多看了你一眼

    对SQLHelper,还是有一点陌生的,但是大多数人都在使用,我就有一种想了解并使用的意愿,于是查了很多资料,发现一片不错的博客,放在下面,作为自己或读者使用的材料. 一.SqlHelper 出场 不 ...

  5. 【BZOJ 2124】【CodeVS 1283】等差子序列

    http://www.lydsy.com/JudgeOnline/problem.php?id=2124 http://codevs.cn/problem/1283/ 重点是把判断是否存在3个数组成等 ...

  6. luogu P1016 旅行家的预算

    题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能行驶的距离D2.出发点每升汽油价格P和沿 ...

  7. luogu P1047 校门外的树

    题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,……,L,都种 ...

  8. HDU 6035 Colorful Tree (树形DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6035 [题目大意] 给出一颗树,一条路径的价值为其上点权的种类数,求路径总价值 [题解] 我们计算 ...

  9. 交换x,y的三种方式

    1 值传递: #include<iostream> using namespace std; int main(){ void change(int ,int); int x=2,y=3; ...

  10. linux shell实现随机数多种方法(date,random,uuid)

    参考: http://www.cnblogs.com/chengmo/archive/2010/10/23/1858879.html $ cat /proc/sys/kernel/random/uui ...