HDOJ题目2861 Stools(递推)

酒吧凳子排列问题
探讨了一个涉及N个凳子、M个顾客及K个间隔的排列组合问题,通过动态规划算法求解特定条件下可能的排列数量。

Stools

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 626    Accepted Submission(s): 227


Problem Description
Patti and Terri run a bar in which there are 15 stools. One day, Darrell entered the bar and found that the situation how customers chose the stools were as follows:
OOEOOOOEEEOOOEO
O means that the stool in a certain position is used, while E means that the stool in a certain position is empty (here what we care is not who sits on the stool, but whether the stool is empty).As the example we show above, we can say the situation how the 15 stools is used determines 7 intervals (as following):
OO E OOOO EEE OOO E O

Now we postulate that there are N stools and M customers, which make up K intervals. How many arrangements do you think will satisfy the condition?
 

Input
There are multi test cases and for each test case:
Each case contains three integers N (0<N<=200), M (M<=N), K (K<=20).
 

Output
For each test case print the number of arrangements as described above. (All answers is fit in 64-bit.)
 

Sample Input
  
3 1 3 4 2 4
 

Sample Output
  
1 2
 

Source
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:   2858  2860  2859  2856  2854 
 

a[n][m][k]=a[n-1][m][k]+b[n-1][m][k-1];
    b[n][m][k]=a[n-1][m-1][k-1]+b[n-1][m-1][k];
    ans[n][m][k]=a[n][m][k]+b[n][m][k]。

    a[n][m][k]:n个座位、m个人、k段、最后一个位置么有人,的情况数;

    b[n][m][k]:n个座位、m个人、k段、最后一个位置有人,的情况数。

ac代码

#include<stdio.h>
#include<string.h>
__int64 a[220][220][22],b[220][220][22];
void init()
{
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	a[1][0][1]=1;
	b[1][1][1]=1;
	int i,j,k;
	for(i=2;i<=200;i++)
	{
		for(j=0;j<=i;j++)
		{
			for(k=1;k<=i&&k<22;k++)
			{
				a[i][j][k]=a[i-1][j][k]+b[i-1][j][k-1];
				if(j)
					b[i][j][k]=a[i-1][j-1][k-1]+b[i-1][j-1][k];
			}
		}
	}
}
int main()
{
	init();
	int n,m,k;
	while(scanf("%d%d%d",&n,&m,&k)!=EOF)
	{
		printf("%I64d\n",a[n][m][k]+b[n][m][k]);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值