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 ...
随机推荐
- matlab学习之绘制参数曲线,添加辅助线以及颜色设置
粘贴代码 % 插入参数曲线h % 插入辅助线h1 % 并设置颜色,包括画布颜色和曲线颜色 t=-pi:0.1:pi; x=cos(t)-sin(3*t); y=sin(t).*cos(t)-cos(3 ...
- CSS 笔记——背景布局
4. 背景布局 -> 背景 (1)background 基本语法 background : background-color || background-image || background- ...
- CSS 笔记——选择器
1. 选择器 (1)类型选择器(标签选择器) 基本语法 E { sRules } 使用说明 类型选择器.以文档对象(Element)类型作为选择器. 选择面较大,适合做某种标签元素外观的常规设置. 代 ...
- [BZOJ1913][APIO2010]信号覆盖(计算几何+计数)
1913: [Apio2010]signaling 信号覆盖 Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1658 Solved: 672[Subm ...
- python基础之类与对象,继承与派生
类与对象 对象的本质也就是一个名称空间而已,用于存放自己独有的属性,而类中存放的是对象共有的属性. __init__会在调用类时自动触发 调用类时发生两件事: 1.创建一个空对象stu1 2.自动触发 ...
- [POI2008]Triangles
题目大意: 平面直角坐标系上有n个点,问以这n个点为顶点的不同的三角形的面积和是多少? 思路: 很容易想到一个O(n^3)的暴力,枚举三个点,用海龙公式求一下面积和即可,这样做是40分. 标算的复杂度 ...
- iOS 在系统设置中展示Version, Build, Git等信息
在设置中,展示自定义内容,如下图INFO区域内容: 步骤: 1.在项目中添加Settings.bundle文件 Root.plist和Root.plist的Source code如下 ...
- hdu3401 Trade 单调队列优化dp
Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 明尼苏达推荐系统导论(第一课 欢迎来到RS)
一.RS介绍 1.显示评分:直接从用户来 隐式评分:从用户活动推测得到的 2.预测是偏好的估计,是预测缺失值,推荐是从其他用户推荐项目,是推荐感兴趣的项目. 3.协同表示利用其它用户的数据 二.欢迎来 ...
- 关于热插拔usb hotplug /proc/sys/kernel mdev udev b...
转:http://www.360doc.com/content/10/0527/18/9922_29835045.shtml 这篇文章说的很好http://blog.chinaunix.net/u1/ ...