Codeforces-118D Caesar's Legions

aius 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 notbeautiful 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 n1n2k1k2 (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 k1footmen 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
Input
2 4 1 1
Output
0
Note

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: 1212212212212122122122121


把n1个步兵和n2个骑兵派成一列,已知连续的步兵不超过k1个,连续的骑兵不超过k2个,求总可能排列情况数

定义dp[i][j][2],指使用i个步兵,j个骑兵的排列。0代表排头为步兵,1代表排头为骑兵

学长本来挂的记忆化搜索的题,但题解没看懂,所以就看了这个用dp做的(也不是很懂)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MOD 100000000
using namespace std;
typedef long long LL;

int N1, N2, K1, K2;
const int maxn = 110;
int dp[maxn][maxn][2];

int main() {
    scanf("%d%d%d%d", &N1, &N2, &K1, &K2);
    for (int i = 0; i <= K1; i++) dp[i][0][0] = 1;
    for (int i = 0; i <= K2; i++) dp[0][i][1] = 1;
    for (int i = 1; i <= N1; i++) {
        for (int j = 1; j <= N2; j++) {
            for (int k = 1; k <= min(i, K1); k++) {
                dp[i][j][0] = (dp[i][j][0] + dp[i - k][j][1]) % MOD;
            }
            for (int k = 1; k <= min(j, K2); k++) {
                dp[i][j][1] = (dp[i][j][1] + dp[i][j - k][0]) % MOD;
            }
        }
    }
    int ans = (dp[N1][N2][0] + dp[N1][N2][1]) % MOD;
    printf("%d\n", ans);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/asd1637579311/article/details/80114315