Caesar‘s Legions
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status
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
Input
2 1 1 10
Output
1
Input
2 3 1 2
Output
5
Input
2 4 1 1
Output
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
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 long long ans; 6 long long dp1[110][110][12]; 7 long long dp2[110][110][12]; 8 int main() 9 { 10 int n1,n2,k1,k2,i,j,k; 11 while(scanf("%d%d%d%d",&n1,&n2,&k1,&k2)!=EOF){ 12 memset(dp1,0,sizeof(dp1)); 13 memset(dp2,0,sizeof(dp2)); 14 dp1[1][0][1]=1; dp2[0][1][1]=1; 15 for(i=0;i<=n1;i++){ 16 for(j=0;j<=n2;j++){ 17 for(k=1;k<=k1;k++){ 18 if(dp1[i][j][k]){ 19 if(k!=k1){ 20 dp1[i+1][j][k+1]+=dp1[i][j][k]; 21 dp1[i+1][j][k+1]%=100000000; 22 } 23 dp2[i][j+1][1]+=dp1[i][j][k]; 24 dp2[i][j+1][1]%=100000000; 25 } 26 } 27 for(k=1;k<=k2;k++){ 28 if(dp2[i][j][k]){ 29 if(k!=k2){ 30 dp2[i][j+1][k+1]+=dp2[i][j][k]; 31 dp2[i][j+1][k+1]%=100000000; 32 } 33 dp1[i+1][j][1]+=dp2[i][j][k]; 34 dp1[i+1][j][1]%=100000000; 35 } 36 } 37 } 38 } 39 ans=0; 40 for(i=1;i<=k1;i++){ 41 ans+=dp1[n1][n2][i]; 42 ans%=100000000; 43 } 44 for(i=1;i<=k2;i++){ 45 ans+=dp2[n1][n2][i]; 46 ans%=100000000; 47 } 48 printf("%I64d\n",ans); 49 } 50 return 0; 51 }
Caesar's Legions(dp)