Caesar's Legions(三维dp)
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
The problem is solved lazy dynamics. Let z[n1] [n2] [2] - a number of ways to place troops in a legion of Caesar. Indicate the following parameters, n1 – is a number of footmen, n2 – is a number of horseman, the third parameter indicates what troops put Caesar in the beginning of the line. If Caesar wants to put the footmen, the state dynamics of the z [n1] [n2] [0] go to the state
z [n1 - i] [n2] [0], where 0 <= i <= min (k1, n1) . If Caesar wants to put the riders, the state dynamics of the z [n1] [n2] [1] go to the state z [n1] [n2 - i] [1], where 0 <= I <= min (k2, n2) .
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 const int mod = 100000000 ; 6 int n1 , n2 , k1 , k2 ; 7 int a[110][110][2] ; 8 9 int Caesar (int n1 , int n2 , int f) 10 { 11 if (a[n1][n2][f] != -1 ) { 12 return a[n1][n2][f] ; 13 } 14 if (n1 + n2 == 0) { 15 a[n1][n2][f] = 1 % mod ; 16 return a[n1][n2][f] ; 17 } 18 a[n1][n2][f] = 0 ; 19 int i ; 20 if (f == 0) { 21 for (i = 1 ; i <= min (k1 , n1 ) ; i++) { 22 a[n1][n2][f] += Caesar (n1 - i , n2 , 1 - f) ; 23 a[n1][n2][f] %= mod ; 24 } 25 } 26 else { 27 for (i = 1 ; i <= min (k2 , n2 ) ; i++) { 28 a[n1][n2][f] += Caesar (n1 , n2 - i , 1 - f ) ; 29 a[n1][n2][f] %= mod ; 30 } 31 } 32 return a[n1][n2][f] ; 33 } 34 35 void solve () 36 { 37 memset (a , 0xFF , sizeof(a) ) ; 38 printf ("%d\n" , ( Caesar (n1 , n2 , 0 ) + Caesar (n1 , n2 , 1 ) ) % mod ) ; 39 } 40 41 int main () 42 { 43 #ifdef online_jude 44 freopen ("a.txt" , "r" , stdin ) ; 45 #endif // online_jude 46 scanf ("%d%d%d%d" , &n1 , &n2 , &k1 , &k2 ) ; 47 solve () ; 48 return 0 ; 49 }