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
1
2 3 1 2
5
2 4 1 1
0
Hint
Let's mark a footman as 1, and a horseman as 2.
In the first sample the only beautiful line-up is: 121
In the second sample 5 beautiful line-ups exist: 12122, 12212, 21212, 21221, 22121
动态规划还得好好研究研究
#include<iostream>
#include<algorithm>
#include<math.h>
#include<cstdio>
#include<string>
#include<string.h>
using namespace std;
const int maxn = 105;
const int base = 100000000;
int n, m, k1, k2, i, j, k, f[maxn][maxn][12][2], sum;
int main(){
while (~scanf("%d%d%d%d", &n, &m, &k1, &k2))
{
memset(f, 0, sizeof(f));
f[1][0][1][0] = 1;
f[0][1][1][1] = 1;
for (i = 0; i <= n;i++)
for (j = 0; j <= m; j++)
if (i + j >1)
{
for (k = 2; k <= k1; k++)
if (i) f[i][j][k][0] = f[i - 1][j][k - 1][0];
for (k = 2; k <= k2; k++)
if (j) f[i][j][k][1] = f[i][j - 1][k - 1][1];
for (k = 1; k <= k2; k++)
if (i) f[i][j][1][0] = (f[i][j][1][0] + f[i - 1][j][k][1]) % base;
for (k = 1; k <= k1; k++)
if (j) f[i][j][1][1] = (f[i][j - 1][k][0] + f[i][j][1][1]) % base;
}
for (sum = 0, i = 1; i <= max(k1, k2); i++) sum = (sum + f[n][m][i][0] + f[n][m][i][1]) % base;
printf("%d\n", sum);
}
return 0;
}

本文探讨了一个有趣的排列组合问题,即如何计算将军队中步兵与骑兵以特定限制条件美丽地排列的方法数量。采用动态规划方法解决该问题,并给出具体实现代码。
279

被折叠的 条评论
为什么被折叠?



