UVa11420 - Chest of Drawers(动态规划)

Problem D
Chest of Drawers
Input:
Standard Input

Output: Standard Output

 

Figure 1: A chest of drawers

A chest of drawers means awardrobe which has many drawers aligned vertically as shown in the figure onthe left. Although this is useful furniture, some problems arise when all thedrawers need have provisions of locking - that is sometimes a drawer is notsecured even if it is locked. For example assume that the third drawer from thetop is locked but the drawer immediately above it is not locked. Then thedrawer that is locked is also not secured because one can access it by pullingout the drawer immediately above it.

 

In a chest of n drawers, thereare a number of ways to ensure that exactly s drawers are secure. For examplefor the chest of drawers shown on the left, exactly four drawers can be securedin six ways. These six ways are shown in Figure 2.

 

Given the value of n and s, yourjob is to find out in how many ways they can be secured.

 

 

 

 

 

 

 

Figure 2: In this figure L means that the drawer is locked and U means that the corresponding drawer is unlocked. And here all six locking combinations are shown which ensures that exactly four drawers are secured. Letters corresponding the secured drawers are boldfaced.

 

Input

The input file contains at most5000 lines of inputs.

 

Each line contains two integers n and s (1 ≤ n ≤ 65 and 0 ≤ s ≤65). Here n is the total number of drawers and s is the number of drawers thatneeds to be secured.

 

Input is terminated by a linecontaining two negative numbers. This input should not be processed.

 

Output

For each line of input produceone line of output. This line contains an integer which denotes in how many, sdrawers out of the n drawers can be secured.

 

Sample Input                            Output for SampleInput

6 2

6 3

6 4

-1 -1

16

9

6

 


#include <cstdio>
#include <cstring>

using namespace std;

typedef long long LL;

const int N = 70;
const int M = 2;

LL memo[N][M][N];
int n, s;

bool input();
void solve();
LL dfs(int dep, int lock, int seq);


int main()
{
	#ifndef ONLINE_JUDGE
		freopen("d:\\OJ\\uva_in.txt", "r", stdin);
	#endif
	
	while (input()) {
		solve();
	}
	return 0;
}

bool input()
{
	scanf("%d%d", &n, &s);
	if (n < 0 && s < 0) return false;
	
	return true;
}

LL dfs(int dep, int lock, int seq)
{
	if (dep == n) return seq == s;
	
	LL& ans = memo[dep][lock][seq];
	if (ans != -1) return ans;
	
	ans = 0;
	if (!lock) {
		ans += dfs(dep + 1, 1, seq) + dfs(dep + 1, 0, seq); 
	} else {
		ans += dfs(dep + 1, 1, seq + 1) + dfs(dep + 1, 0, seq);
	}
	
	return ans;
}

void solve()
{
	memset(memo, -1, sizeof(memo));
	
	printf("%lld\n", dfs(1, 0, 0) + dfs(1, 1, 1));
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值