{DP!}ZOJ 2604

这个题的题意给出了很多限制

给出的n是开壶的个数

并且都是成对出现的

dp[i][j]i为n,j为小于等于k

dp[i][1]遍都是一种,都是()()()()()()()()()这种形式

其次会出现(((())))()()()()   ()()()(()()()))()()这种形式

总有一个最大深度的

把第一个看成特殊的!!!

把每一个分成了两种情况(X)Y

推dp[i][j] 的时候就根据这个式子

dp[i][j]  +=    (X的种数)dp[k-1][j-1] * (Y的种数)dp[i-k][j]

其中只要i-k + k-1 + 1的和就为开壶的对数i即可

所以可以对调

/*

dp1 1+=dp0 1dp00

1 += 1 * 1

1

dp2 1+=dp1 1dp00

1 += 1 * 1

1

dp2 1+=dp0 1dp10

1 += 1 * 0

1

*/

n = 52
dp = [[1 if i == 0 else 0 for j in range(n)] for i in range(n)]
for i in range(1, n):
  for j in range(1, n):
    for k in range(1, i + 1):
      dp[i][j] += dp[i - k][j] * dp[k - 1][j - 1]

ri = 0
while True:
  i, j = map(int, raw_input().split())
  if i == 0 or j == 0:
    break
  if ri > 0:
    print
  ri += 1
  print ("%s %d: %d" % ('Case', ri, dp[i][j] - dp[i][j - 1]) )
import java.util.*;
import java.math.*;
public class Main{
    static BigInteger dp[][]=new BigInteger[55][55];
    public static void f(){
    for(int i=0;i<=52;i++)
        for(int j=0;j<=52;j++)
            dp[i][j] = BigInteger.ZERO;
    for(int j=0;j<=52;j++)
            dp[0][j] = BigInteger.ONE;
    for(int i=1;i<=52;i++)
        for(int j=1;j<=52;j++)
            for(int k=1;k<=i;k++)
            dp[i][j] = dp[i][j].add( dp[k-1][j-1].multiply(dp[i-k][j]) );
    }
    public static void main(String[] args)
    {
        f();
        Scanner in = new Scanner(System.in);
        int kase=0;
        while(in.hasNext()){

            int n=in.nextInt();
            int k=in.nextInt();
         if(n==0&&k==0) break;
         if(kase!=0) System.out.println("");
            System.out.println("Case "+(++kase)+": "+( dp[n][k].subtract(dp[n][k-1]) ) );
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 20:48:58

{DP!}ZOJ 2604的相关文章

[dp] zoj 3769 Diablo III

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3769 Diablo III Time Limit: 2 Seconds      Memory Limit: 65536 KB Diablo III is an action role-playing video game. A few days ago, Reaper of Souls (ROS), the new expansion of Diablo I

[dp] zoj 3740 Water Level

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5122 Water Level Time Limit: 2 Seconds      Memory Limit: 65536 KB Hangzhou is a beautiful city, especially the West Lake. Recently, the water level of the West Lake got lower and lower

[递推dp] zoj 3747 Attack on Titans

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5170 Attack on Titans Time Limit: 2 Seconds      Memory Limit: 65536 KB Over centuries ago, mankind faced a new enemy, the Titans. The difference of power between mankind and their newf

[dp] zoj 3682 E - Cup 3

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3682 E - Cup 3 Time Limit: 3 Seconds      Memory Limit: 65536 KB The 2012 Europe Cup was over and Spain won the Championship. The fans of Spain want to hold some activities to celebra

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

(状态压缩DP) zoj 3471

T - Most Powerful Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3471 Appoint description:  System Crawler  (2015-04-16) Description Recently, researchers on Mars have discovered N powerful atoms

状压DP [ZOJ 3471] Most Powerful

Most Powerful Time Limit: 2 Seconds      Memory Limit: 65536 KB Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a

DP ZOJ 3872 Beauty of Array

题目传送门 1 /* 2 DP:dp 表示当前输入的x前的包含x的子序列的和, 3 求和方法是找到之前出现x的位置(a[x])的区间内的子序列: 4 sum 表示当前输入x前的所有和: 5 a[x] 表示id: 6 详细解释:http://blog.csdn.net/u013050857/article/details/45285515 7 */ 8 #include <cstdio> 9 #include <algorithm> 10 #include <cmath>

(环形DP) zoj 3310

W - Unrequited Love Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3310 Appoint description:  System Crawler  (2015-04-16) Description Owen had been through unrequited love with N MM for a long t