ZOJ 2604 Little Brackets DP
2017-04-28 19:39
393 查看
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
Sample Output
Author: Andrew Stankevich
Source: Andrew Stankevich's Contest #7
边界条件: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 [k].subtract(dp [k-1])); pr=true; } } }
相关文章推荐
- ZOJ 2604 Little Brackets(高精度+dp)
- ZOJ 2604 Little Brackets DP
- zoj 2604 Little Brackets(动态规划+递推+java)
- {DP!}ZOJ 2604
- ZJU 2604 Little Brackets - 动态规划 Catalan数变形
- zoj 2972 Hurdles of 110m (DP)
- zoj_3735,dp,长沙站j题
- 【题解】 zoj 3747 计数dp 进击的巨人
- ZOJ-3329 One Person Game(期望dp)
- ZOJ 3494 BCD Code AC自动机 + 数位DP
- HDOJ 1081 POJ 1050 ZOJ 1074 TO THE MAX【dp】
- ZOJ 3211 Dream City(DP)
- ZOJ 3735 Cake(区间DP,最优三角剖分)
- zoj 3551 Bloodsucker 概率DP
- poj 2955 Brackets 区间dp
- ZOJ 3777 Problem Arrangement (状压DP)
- CF# 149 D Coloring Brackets(区间dp)
- ZOJ 3949 Edge to the Root(树形DP)
- ZOJ Most Powerful (状态压缩DP)
- POJ 2955 Brackets(区间DP)