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 ...
随机推荐
- HashMap的实现原理 HashMap底层实现,hashCode如何对应bucket?
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 数组和链表组合成的链表散列结构,通过hash算法,尽量将数组中的数据分布均匀,如果has ...
- 手机发送验证码—.net代码
注册过程中,短信发送验证码流程如下: (1).用户提交手机号码,申请湖区验证码 (2).网站按照预制规则生成验证码 (3).网站将用户手机号码和验证码发送到短信平台 (4).将制定内容发送到制定手机号 ...
- 【(博弈)dfs序+树状数组】BZOJ2819-Nim
[题目大意] 普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可以不取.谁不能取谁输.这个游戏是有必胜策略的.现在对每一堆编号1,2,3,4,...n,在 ...
- RMI(Remote Method Invocation ) 概念恢复
1.RMI是远程方法调用的简称,像其名称暗示的那样,它能够帮助我们查找并执行远程对象,通俗的说,远程调用就像一个class放在A机器上,然后在B机器中调用这个class的方法. 2.EMI术语 在研究 ...
- Problem X: 双层金字塔
#include<stdio.h> int main() { int i,j,n,m; while(scanf("%d",&n)!=EOF) { ;i<= ...
- ArcGIS10.1如何将数据库注册到ARCSERVER服务器
原文链接:http://www.cnblogs.com/hanchan/archive/2013/09/24/3337034.html 一.了解ArcGIS Server以及如何利用ArcServer ...
- 论MORMOT序列的JSON格式
论MORMOT序列的JSON格式 JSON 数据使用 UTF-8 编码 BLOB 字段值会用 Base64编码 JSON数据是一个对象数组: [ {"col1":val11,&qu ...
- Express重定向
var express = require('express'); var app = express(); app.get('/',function(req,res){ res.redirect(' ...
- nodejs调试利器:supervisor
测试多了,是不是感觉每次要重新node一次app.js,很烦恼? 用supervisor,只有有改动,页面刷新就可以看到效果,不用重启node.js 安装: npm -g install superv ...
- [转]MSSQL多列取最大或者最小值
本文转自:http://blog.csdn.net/wufeng4552/article/details/4681510 /* lvl1 lvl2 lvl3 lvl4 lvl 4 3 4 1 3 2 ...