把n1个步兵和n2个骑兵派成一列,已知连续的步兵不超过k1个,连续的骑兵不超过k2个,求总可能排列情况数
定义dp[i][j][2],指使用i个步兵,j个骑兵的排列。0代表排头为步兵,1代表排头为骑兵
#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; }