Codeforces118D Caesar's Legions(DP)

题目

Source

http://codeforces.com/problemset/problem/118/D

Description

Gaius Julius Caesar, a famous general, loved to line up his soldiers. Overall the army had n1 footmen and n2 horsemen. Caesar thought that an arrangement is not beautiful if somewhere in the line there are strictly more that k1 footmen standing successively one after another, or there are strictly more than k2 horsemen standing successively one after another. Find the number of beautiful arrangements of the soldiers.

Note that all n1 + n2 warriors should be present at each arrangement. All footmen are considered indistinguishable among themselves. Similarly, all horsemen are considered indistinguishable among themselves.

Input

The only line contains four space-separated integers n1, n2, k1, k2 (1 ≤ n1, n2 ≤ 100, 1 ≤ k1, k2 ≤ 10) which represent how many footmen and horsemen there are and the largest acceptable number of footmen and horsemen standing in succession, correspondingly.

Output

Print the number of beautiful arrangements of the army modulo 100000000 (108). That is, print the number of such ways to line up the soldiers, that no more than k1 footmen stand successively, and no more than k2 horsemen stand successively.

Sample Input

2 1 1 10
2 3 1 2
2 4 1 1

Sample Output

1
5
0

 

分析

题目大概说有n1个步兵和n2骑兵要排成一排,连续步兵数不能超过k1个,连续骑兵数不能超过k2个,问有几种排列方案。

 

  • dp[i][j][x][y]表示已经有i个步兵j个骑兵参与排列且末尾有x个连续步兵或y个连续骑兵的方案数
  • 转移就是通过在末尾放上步兵或者骑兵转移

 

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int d[111][111][11][11];
int main(){
	int n1,n2,k1,k2;
	scanf("%d%d%d%d",&n1,&n2,&k1,&k2);
	d[0][0][0][0]=1;
	for(int i=0; i<=n1; ++i){
		for(int j=0; j<=n2; ++j){
			for(int x=0; x<=k1; ++x){
				for(int y=0; y<=k2; ++y){
					if(d[i][j][x][y]==0) continue;
					if(i!=n1 && x!=k1){
						d[i+1][j][x+1][0]+=d[i][j][x][y];
						d[i+1][j][x+1][0]%=100000000;
					}
					if(j!=n2 && y!=k2){
						d[i][j+1][0][y+1]+=d[i][j][x][y];
						d[i][j+1][0][y+1]%=100000000;
					}
				}
			}
		}
	}
	int ans=0;
	for(int i=1; i<=k1; ++i) ans+=d[n1][n2][i][0],ans%=100000000;
	for(int i=1; i<=k2; ++i) ans+=d[n1][n2][0][i],ans%=100000000;
	printf("%d",ans);
	return 0;
}

 

转载于:https://www.cnblogs.com/WABoss/p/5877044.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值