Zoj 2604 little brackets DP

Source: Internet
Author: User


DP:

  • Boundary Condition: DP [0] [J] = 1
  • Recursive Formula: DP [I] [J] = sum {DP [I-K] [J] * DP [k-1] [J-1] | 0 <k ≤ I}
I pairs of parentheses depth does not exceed J, can be uniquely expressed as (x) y form, where X and Y can be empty, set X with K-1 pair brackets, then the number of corresponding solutions is 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 "((()(())())) "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 shocould 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 237 230 0

Sample output

Case 1: 3Case 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.