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;
}

 

posted @ 2016-09-16 19:53  WABoss  阅读(719)  评论(0编辑  收藏  举报