Codeforces Beta Round #89 (Div. 2): D. Caesar's Legions(DP)

探讨了给定数量的步兵和骑兵,在限定连续相同兵种的最大数量条件下,求所有合法排列的数量。通过动态规划方法解决了这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

D. Caesar's Legions

time limit per test 2 seconds

memory limit per test 256 megabytes

input standard input

output standard output

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.

Examples

input

2 1 1 10

output

1

input

2 3 1 2

output

5

 

题意:n个0,m个1,问有多少种排列方式满足连续的0不超过a个,连续的1不超过b个

dp[x][y][z] = 前x个数中有y个1,并且最后一个数是z的方案数

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
#define LL long long
#define mod 100000000
LL dp[205][105][2];
int main(void)
{
	int n, m, a, b, i, j, k;
	scanf("%d%d%d%d", &n, &m, &a, &b);
	n = n+m, dp[0][0][1] = dp[0][0][0] = 1;
	for(i=1;i<=n;i++)
	{
		for(j=0;j<=m;j++)
		{
			for(k=i-1;k>=max(i-a,0);k--)
				dp[i][j][0] = (dp[i][j][0]+dp[k][j][1])%mod;
			for(k=i-1;k>=max(i-b,max(i-j,0));k--)
				dp[i][j][1] = (dp[i][j][1]+dp[k][j-(i-k)][0])%mod;
		}
	}
	printf("%lld\n", (dp[n][m][0]+dp[n][m][1])%mod);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值