We need to compute number of sequences of length n1+n2 with n1 copies of symbol F (footman) and n2 copies of H (horseman), such that no run of consecutive F's exceeds k1, and no run of consecutive H's exceeds k2. All arrangements satisfying the run constraints. Count modulo MOD=100000000 (10^8).
Constraints: n1,n2 up to 100; k1,k2 up to 10.
We can use DP. Let dp[i][j][c][l] where i = number of footmen used so far, j = number of horsemen used so far, c = last type (0 for footmen, 1 for horsemen), and l = length of the current run of that type (1 <= l <= k_c). But we can combine c and l. Or we can use DP with two arrays: dpF[i][j][l] = number of sequences that end with footmen, with l consecutive footmen at the end (1 <= l <= k1), and using i footmen and j horsemen total. Similarly dpH[i][j][l] for ending with horsemen and run length l of H. Then transition:
From dpF[i][j][l] we can add another footman if l < k1 and i < n1 => dpF[i+1][j][l+1] += dpF[i][j][l].
Or add a horseman (start new run) => dpH[i][j+1][1] += dpF[i][j][l].
Similarly, from dpH[i][j][l] we can add another horseman if l < k2 and j < n2 => dpH[i][j+1][l+1] += dpH[i][j][l].
Or add a footman => dpF[i+1][j][1] += dpH[i][j][l].
Initial state: we can start with footman if n1>0: dpF[1][0][1] = 1; and start with horseman if n2>0: dpH[0][1][1] = 1.
Then answer = sum_{l=1..k1} dpF[n1][n2][l] + sum_{l=1..k2} dpH[n1][n2][l] (mod MOD).
DP dimension: i up to 100, j up to 100, l up to max(k1,k2) <=10, so 10010010 = 100k * 2 = 200k states, which is fine.
But we need to be careful: we need to iterate over i and j, and for each l update. Complexity: O(n1n2max(k1,k2)). 10010010=100k, negligible.
Alternatively, we can use DP where we just keep number of ways to arrange i footmen and j horsemen with no constraints on the end run, but with the ability to add items while tracking last run length. Our approach is straightforward.
Edge Cases: k1 or k2 might be large relative to n1/n2 but constraints small, but still fine. If k1 < 1? but constraints say at least 1, so always at least 1 allowed. However, if k1=0 would be impossible, but not case.
The answer modulo 10^8.
Check sample:
-
n1=2,n2=1,k1=1,k2=10. So max footmen in a row =1. So sequences of two F's and one H. With run constraint: cannot have two F's consecutive. So must be F H F or F F H? But F F H has two consecutive F's -> invalid. H F F also invalid. So only F H F => 121. Our DP should output 1.
-
n1=2,n2=3,k1=1,k2=2. They got 5.
Let's test: sequences of length 5 with 2 footmen and 3 horsemen, with no more than 1 footman in a row, and no more than 2 horsemen in a row. That means footmen must be isolated, no two footmen adjacent. So they are placed among horsemen. Horsemen runs can be at most 2. So we need to count placements of 2 F among a sequence of H's such that no two F adjacent and runs of H's between them not exceeding 2.
We can enumerate: total arrangements with 2 F and 3 H, F's not adjacent. Let's represent positions of F's. Since there are 5 slots, choose 2 positions for F such that they are not adjacent. That yields C(5,2)=10, minus adjacents: positions (1,2),(2,3),(3,4),(4,5) => 4, so 6 sequences. But we also have the constraint that H runs <=2. Let's check each of the 6: List sequences with F=1, H=2.
Possible placements (positions of F): (1,3): 1 2 1 2 2? Wait we need 2 F and 3 H: sequence indices 1 to 5. With F positions at (1,3): then H's at 2,4,5 => but order: 1:F,2:H,3:F,4:H,5:H -> sequence = F H F H H -> 12122. Check runs: H after F at pos2: run length 1; after pos4: two consecutive H's (pos4-5) run length 2, allowed <=2. Footmen runs: each single. So valid.
(1,4): F at 1,4; H at 2,3,5 -> sequence: F H H F H -> 12212. Runs: H at 2-3 run length 2 allowed; F at 1 single; F at 4 single; H at 5 single. Valid.
(1,5): F at 1,5; H at 2,3,4 -> sequence: F H H H F -> 12221? That gives H run length 3, exceeds 2 -> invalid.
(2,4): F at 2,4; H at 1,3,5 -> sequence: H F H F H -> 21212. H runs: at start single, between F's single, at end single. Valid.
(2,5): F at 2,5; H at 1,3,4 -> sequence: H F H H F -> 21221? Actually order: 1:H,2:F,3:H,4:H,5:F -> 21221? Wait symbols: 2 (H),1 (F),2 (H),2 (H),1 (F) -> actually footmen as 1, horsemen as 2, so 2 1 2 2 1 = 21221. H runs: at start length1, after pos3-4 length2, okay. Valid.
(3,5): F at 3,5; H at 1,2,4 -> sequence: H H F H F -> 22121. H runs: first two H's length2 allowed; then after F at pos4 a single H; valid.
So indeed 5 valid. So DP should output 5.
- n1=2,n2=4,k1=1,k2=1. Constraint: no more than 1 footman consecutively (so F's must be isolated) and no more than 1 horseman consecutively (so H's must be isolated). So we need a sequence of 2 F and 4 H such that no two same consecutive. That's impossible because total length 6, counts difference? For alternating, we need either equal or differ by 1. For alternating with two types, maximum length without repeats is 2*min+1 if difference >0. With 2 and 4, min=2, so max length without repeats is 5? Actually alternating starting with F: F H F H H? Wait we need to avoid consecutive same. The only sequences with no two identical adjacent are those where the numbers differ by at most 1. Since |n1-n2| = 2 >1, impossible. So answer 0. Good.
Our DP will handle.
Implementation details: Use 3D arrays: dpF[i][j][l] and dpH[i][j][l]. But we can compress dimension j or i. Since n1,n2 <=100, we can use arrays of size [101][101][11] maybe with k1 up to 10, so max run length 10; but careful: l up to k1 for F, and up to k2 for H. But we can just allocate size 11 for l.
We'll iterate i from 0..n1, j from 0..n2. But careful: we need to update in increasing order of total used: i+j. So we can loop i from 0 to n1, j from 0 to n2, and for each state update transitions that increase either i or j. This is like DP over counts. But careful: when we process dpF[i][j][l], we need to ensure that we have already computed states with that (i,j). Since we will iterate i from 0 to n1 and j from 0 to n2, and update to states with i+1 or j+1 (i+1 > i or j+1 > j). So that's safe.
We need to set base: dpF[1][0][1] = 1 if n1>0; dpH[0][1][1] = 1 if n2>0. But also we could start with dpF[0][0][0]? Not needed.
Alternatively, we can start with empty sequence (no last type) and then add first element as both possibilities. But our base initialization works.
Then loop i from 0 to n1, j from 0 to n2:
for l from 1..k1: if dpF[i][j][l] >0:
// add footman
if i < n1 and l < k1:
dpF[i+1][j][l+1] = (dpF[i+1][j][l+1] + dpF[i][j][l]) % MOD
// add horseman
if j < n2:
dpH[i][j+1][1] = (dpH[i][j+1][1] + dpF[i][j][l]) % MOD
for l from 1..k2: if dpH[i][j][l] >0:
// add horseman
if j < n2 and l < k2:
dpH[i][j+1][l+1] = (dpH[i][j+1][l+1] + dpH[i][j][l]) % MOD
// add footman
if i < n1:
dpF[i+1][j][1] = (dpF[i+1][j][1] + dpH[i][j][l]) % MOD
But we need to be careful: dpF[i][j][l] might be defined only for i>=1, but we can have i=0, j=0, dpF[0][0][*] not defined. We'll only have base states. So we start with i=0..n1, but dpF at i=0 only possibly when j>0? Actually base state dpH[0][1][1] =1, so dpF[0][1][?] none. So in loops, we should check if i>0 for dpF states; but we can just iterate over all i,j and skip if no data.
Alternative: we can incorporate empty state with c=2 and l=0, and then transitions to first foot/horse. That might be cleaner. But our approach is fine.
But we need to ensure we process dpF for i from 0 to n1, but dpF[0][...] will be zero for all j, so it's fine.
We'll allocate arrays with dimensions [n1+1][n2+1][max(k1,k2)+1], but for dpF we only need l up to k1; for dpH l up to k2. We'll allocate separate arrays.
Memory: 10110111 = 112211 per array, times 2 = ~224k, small.
Make sure to use int or long and mod.
Let's test with sample manually.
But there is nuance: When i=0,j=0, there is no base state. We'll add base states manually after initialization. So after we set dpF[1][0][1]=1 and dpH[0][1][1]=1, then loop i from 0..n1, j from 0..n2, we will process these states when we reach i=1,j=0 and i=0,j=1 respectively.
Edge: if n1=0? But constraints say n1,n2 >=1? Actually problem statement: 1 ≤ n1, n2 ≤ 100. So both positive. So we always have at least one of each? Actually n1 and n2 both at least 1, so both types present. But we can still handle if one is zero, but not needed. However, constraints say 1 ≤ n1, n2 ≤ 100, so both >=1. But in sample they have n1=2,n2=1 etc, both at least 1. So fine.
Now check second sample: n1=2,n2=3,k1=1,k2=2. We'll compute DP.
Initialize:
dpF[1][0][1]=1
dpH[0][1][1]=1
Loop:
i=0,j=0: dpF all zero; dpH all zero.
i=0,j=1: we have dpH[0][1][1]=1.
Process dpH[0][1][1]:
if j<3 and l<2 => j<3 true, l<2 true => dpH[0][2][2] +=1
if i<2 => i<2 true => dpF[1][1][1] +=1
So after processing i=0,j=1:
dpH[0][2][2]=1, dpF[1][1][1]=1
i=0,j=2: we have dpH[0][2][2]=1
Process: j<3 and l<2? l=2, k2=2, condition l<k2? Strictly more than k2 are not allowed; but we enforce when adding: we can add another horseman only if l < k2, because if we add one, the run becomes l+1; if l == k2, then l+1 = k2+1 which is not allowed. So condition is if l < k2. So for l=2, k2=2, condition false, cannot add another H.
Add footman: if i<2 => true, dpF[1][2][1] +=1.
So dpF[1][2][1] gets +1.
i=0,j=3: but j=3 is n2=3, no more H to add; but we might have states from earlier? Not from i=0,j=3? We haven't reached yet. Possibly from other transitions? At i=0,j=3, dpH may exist if we added H from earlier? But from i=0,j=2, we didn't add H. So dpH[0][3] maybe not created. Also from i=1,j=2 might go to i=1,j=3. We'll continue.
i=1,j=0: dpF[1][0][1]=1 (base)
Process: i<2 and l<k1? k1=1, l=1, so l<1 false. So cannot add footman.
Add horseman: if j<3 => true, dpH[1][1][1] +=1.
So dpH[1][1][1] =1.
i=1,j=1: we have dpF[1][1][1]=1 and dpH[1][1][1]=1 from previous? Actually we set dpF[1][1][1]=1 from earlier (from i=0,j=1). Also dpH[1][1][1] from i=1,j=0? That we just set. And possibly other contributions? So process:
- dpF[1][1][1]: i=1,j=1,l=1.
i<2? yes, but l<k1? k1=1, l=1 -> false, cannot add F.
j<3? yes, add H: dpH[1][2][1] +=1.
- dpH[1][1][1]:
j<3 and l<k2? l=1<2 true -> dpH[1][2][2]? Wait adding H: dpH[i][j+1][l+1] = dpH[1][2][2] +=1.
i<2? yes -> add F: dpF[2][1][1] +=1.
Thus after i=1,j=1:
dpH[1][2][1] gets +1 from dpF, so =1.
dpH[1][2][2] gets +1 from dpH, so =1.
dpF[2][1][1] gets +1 from dpH, so =1.
i=1,j=2: we have dpF[1][2][1]=1 from earlier (i=0,j=2) and dpH[1][2][1]=1, dpH[1][2][2]=1.
Process each:
- dpF[1][2][1]:
i<2? yes, but l<1? false, cannot add F.
j<3? yes, add H: dpH[1][3][1] +=1.
- dpH[1][2][1]:
j<3? j=2<3 true, l<2 true => dpH[1][3][2] +=1.
i<2? true => dpF[2][2][1] +=1.
- dpH[1][2][2]:
j<3? yes, l<2? l=2 false, cannot add H.
i<2? yes => dpF[2][2][1] +=1. (another +1)
So updates:
dpH[1][3][1] =1.
dpH[1][3][2] =1.
dpF[2][2][1] gets from two: first from dpH[1][2][1] => +1, second from dpH[1][2][2] => +1 => total 2.
i=1,j=3: (j=3 is max). We have dpH[1][3][1]=1, dpH[1][3][2]=1. Process:
- dpH[1][3][1]:
j<3? false (j=3, cannot add H because n2=3 reached). So skip adding H.
i<2? true => dpF[2][3][1] +=1.
- dpH[1][3][2]:
j<3? false (cannot add H)
i<2? true => dpF[2][3][1] +=1.
So dpF[2][3][1] gets +2.
i=2,j=0: dpF[2][0]? Did we have? Possibly from base? Not yet. Actually dpF[2][0] could come from i=1,j=0 adding F? But we couldn't add F because l<k1 false. So not. So dpF[2][0] =0.
i=2,j=1: we have dpF[2][1][1]=1 from earlier (i=1,j=1). Also maybe others. Process:
- dpF[2][1][1]:
i<2? false (i=2=n1) so cannot add F.
j<3? true => add H: dpH[2][2][1] +=1.
i=2,j=2: we have dpF[2][2][1]=2 from earlier. Also maybe dpH[2][2]? We have dpH[2][2][1]? Not yet. Also dpH[2][2][?]. From i=2,j=1 we added dpH[2][2][1]=1. So states: dpF[2][2][1]=2, dpH[2][2][1]=1.
Process:
- dpF[2][2][1]:
i<2? false.
j<3? true => dpH[2][3][1] +=2.
- dpH[2][2][1]:
j<3? true, l<2 true => dpH[2][3][2] +=1.
i<2? false (i=2). So no add F.
Thus dpH[2][3][1] +=2, dpH[2][3][2] +=1.
i=2,j=3: we have dpF[2][3][1]=2 (from i=1,j=3) and dpH[2][3][1] and [2] from above. Process:
- dpF[2][3][1]:
i<2? false.
j<3? false. So no transitions.
- dpH[2][3][1]:
j<3? false.
i<2? false. So no transitions.
- dpH[2][3][2]:
j<3? false.
i<2? false. So none.
Now answer = sum_{l=1..1} dpF[2][3][l] = dpF[2][3][1] =2, plus sum_{l=1..2} dpH[2][3][l] = dpH[2][3][1] + dpH[2][3][2] = 2+1=3. Total =5. Good.
Thus DP works.
Now note modulo 100000000.
Implementation straightforward.
But we might need to consider that k1 and k2 up to 10, but n1,n2 up to 100; run lengths not exceed min(k1, n1) etc. So we can allocate l dimension size up to max(k1,k2)+1. Use int arrays and modulo addition.
Potential pitfalls: We must set initial states correctly: dpF[1][0][1] = 1 if n1>=1; dpH[0][1][1] = 1 if n2>=1. Since both n1,n2>=1, we set both. However, note that if both are set, we are double counting? No, they represent different starting types; that's correct.
But careful: If we set both base states, that's fine; but note there could be a scenario where both base states exist and we later combine? There is no duplication; they are distinct ways.
But what about empty sequence? Not needed.
Alternative approach: start with dp[0][0][0][0] but with c indicating none; but it's simpler as above.
Implementation steps:
Read n1,n2,k1,k2.
Set MOD = 100000000.
Initialize dpF = new int[n1+1][n2+1][k1+1] (since l from 1 to k1, but we can ignore index 0). Actually we can allocate size [n1+1][n2+1][k1+1] and store l=1..k1. Similarly dpH size [n1+1][n2+1][k2+1].
But careful: We may need to access dpF[i][j][l] for l up to k1; if we allocate exactly k1+1, we can use index l directly. So allocate as [n1+1][n2+1][k1+1] but we will only use indices 1..k1.
Similarly dpH.
Initialize all to 0.
If n1>0: dpF[1][0][1]=1;
If n2>0: dpH[0][1][1]=1;
Now loop i from 0 to n1:
for j from 0 to n2:
// process dpF
for l in 1..k1:
val = dpF[i][j][l]
if val == 0: continue
// add footman
if i < n1 and l < k1:
dpF[i+1][j][l+1] = (dpF[i+1][j][l+1] + val) % MOD
// add horseman
if j < n2:
dpH[i][j+1][1] = (dpH[i][j+1][1] + val) % MOD
// process dpH
for l in 1..k2:
val = dpH[i][j][l]
if val == 0: continue
// add horseman
if j < n2 and l < k2:
dpH[i][j+1][l+1] = (dpH[i][j+1][l+1] + val) % MOD
// add footman
if i < n1:
dpF[i+1][j][1] = (dpF[i+1][j][1] + val) % MOD
After loops:
ans = 0
for l=1..k1: ans = (ans + dpF[n1][n2][l]) % MOD
for l=1..k2: ans = (ans + dpH[n1][n2][l]) % MOD
Print ans.
Check that we correctly handle cases where n1=0 or n2=0? Not needed per constraints, but if we want to be safe, the initialization conditions handle: if n1=0, we cannot set dpF[1][0][1] because i=1 > n1, so we skip; but then we need to count sequences with only horsemen, subject to k2. If n1=0 and n2>0, all sequences consist only of H, and the constraint is that no more than k2 H consecutively. But since there's only H, the whole sequence length n2 must not exceed k2? Actually constraint: there are strictly more than k2 horsemen standing successively. So if the entire line is horsemen, that is a run of length n2. So it is allowed only if n2 <= k2. If n2 > k2, then no beautiful arrangement. For our DP, if we start with horseman, we can add up to k2. So dpH[0][j][l] would only exist for l up to k2, and we would get a final state dpH[0][n2][l] only if we can fill exactly n2 with runs that never exceed k2. That's possible only if n2 can be built from segments? But with only one type, the only sequence is all H; thus if we start with H, we can only add H until we hit k2, then we cannot add more because we cannot switch to another type (since no footmen). So the DP would give zero if n2 > k2 because we cannot continue after reaching k2. But wait: if n1=0, we cannot switch to footman, so the only transitions from dpH states are adding another horseman (if l<k2) or switching to footman (not allowed because n1=0, so i<n1 false). So indeed we can only add horseman as long as l<k2. So to reach dpH[0][n2][l], we need to have started at dpH[0][1][1] and added horseman each step, but we cannot exceed l=k2. So if n2 > k2, there is no way to have a sequence of all H because after k2, you can't add more. So DP yields 0. However, is there an arrangement that uses only H but with some other segments? No, because only H exists. So indeed answer should be 1 if n2 <= k2 else 0. Let's check our DP: Starting dpH[0][1][1]=1. Then from that, we can add horseman to get dpH[0][2][2] etc. If n2 <= k2, we can reach dpH[0][n2][n2] (since we can keep adding, and l will equal the count because we never reset). But wait if we add horseman each time, l increments to n2, and we need to be able to add up to n2; but condition to add is l < k2, so to add the last horseman to reach n2, we need that at step j = n2-1, l = n2-1 < k2, i.e., n2-1 < k2 => n2 <= k2. So if n2 == k2, we can still add the last? Actually to go from j=n2-1 to j=n2, we need l = n2-1 < k2, which holds if n2-1 < k2, i.e., n2 <= k2. So if n2 = k2, then to reach j=k2, we need to have l = k2-1 at j=k2-1, which is < k2, so we can add the k2-th H, resulting l = k2. So dpH[0][k2][k2] exists. So answer includes that. So DP correctly gives 1. If n2 < k2, also works. So good.
Similarly for n2=0.
Thus DP works for all.
Now note that we used both initial states dpF and dpH. But if one type count is zero, we should not set its base state because there is no soldier of that type. But as constraints guarantee both >=1, we don't worry. However, for completeness, we can conditionally set.
Now check complexity: loops i 0..100, j 0..100, l up to 10, that's 1e5 iterations, each doing constant updates, fine.
Now we need to be careful about modulo: MOD = 100000000 (1e8). Not 1e9+7. So we use int but sum may exceed int range? 1e8 * something maybe up to about 1e12? Actually number of sequences could be large but we mod each addition, we can use 64-bit long for intermediate then mod. In Java, we can use int and mod after each addition if we ensure sum < 2MOD? Actually addition of two modded numbers < MOD, sum < 2MOD, so we can subtract MOD if needed. But multiple additions could overflow if we keep adding many contributions; but we will do mod each time when we add: newVal = (oldVal + val) % MOD. That's safe with long then cast to int, or use modulo addition.
In Python it's trivial.
But since I'm to write solution in some language? The problem is from codeforces? Usually solutions in C++ or Python. I'll implement in Python for this reasoning.
But ensure we set recursion limit? Not needed.
Now there's also a combinatorial DP approach using generating functions, but not needed.
We'll implement with DP as described.
Edge Cases: When k1 is greater than n1, it doesn't matter; we can have all footmen consecutively; our DP condition l < k1 allows l up to k1-1 to add another footman; but if k1 > n1, we could still have l up to n1; but we only allow l <= k1, so if k1 > n1, still fine, because l will never exceed n1. The condition l < k1 is safe.
Now final answer modulo.
Now we must output as integer.
Let's test with sample 1: n1=2,n2=1,k1=1,k2=10.
Our DP: initialization: dpF[1][0][1]=1; dpH[0][1][1]=1.
Loop:
i=0,j=0: none.
i=0,j=1: dpH[0][1][1]=1:
j<1? n2=1, so j=1 not <1, so cannot add H.
i<2? yes -> add F: dpF[1][1][1] +=1.
So dpF[1][1][1]=1.
i=0,j=2? j max 1, not in loop.
i=1,j=0: dpF[1][0][1]=1:
i<2? yes, but l<k1? l=1, k1=1 -> false, so cannot add F.
j<1? false (j=0<1 true?) Actually j=0 < n2=1 true -> add H: dpH[1][1][1] +=1.
So dpH[1][1][1]=1.
i=1,j=1: we have dpF[1][1][1]=1, dpH[1][1][1]=1.
- dpF: i<2? but l=1, k1=1, cannot add F. j<1? j=1 not <1, cannot add H. So no transition.
- dpH: j<1? false, cannot add H. i<2? true, add F: dpF[2][1][1] +=1.
Thus dpF[2][1][1]=1.
i=2,j=1: we have dpF[2][1][1]=1.
i<2? false; j<1? false. So no transitions.
Now answer: sum dpF[2][1][l] = 1, sum dpH[2][1][l] = 0 => total 1. Good.
Third sample: n1=2,n2=4,k1=1,k2=1.
We can test quickly: but expect 0.
Will DP produce 0? Let's reason: All sequences must alternate 1 and 2 because no two same adjacent. With n1=2, n2=4, can't alternate because difference >1. So DP should yield 0. Our DP will compute limited transitions and eventually at final state (i=2,j=4) no ways. Let's test manually a bit: Starting states: dpF[1][0][1]=1, dpH[0][1][1]=1. Since k1=1 and k2=1, you cannot add two of same type consecutively. So transitions only allow switching type. So essentially we are counting alternating sequences starting with F or H. After first step, each addition must switch type. So we can only have sequences that strictly alternate. So the DP will only propagate if we can switch each time. At each state, adding footman is allowed only if last was H (since we switch). And adding horseman only if last was F. So the DP essentially counts the number of sequences where the count of F and H differ by at most 1, and the last type can be either. Since n1=2,n2=4, difference 2, no such sequence. So answer 0. Good.
Now check a bigger case to ensure mod works.
Now, we might consider an alternative DP using 2D with run lengths limited, but our 3D is fine.
Implementation details: In Python, we can use lists of lists. But we need to allocate dpF as [[[0](k1+1) for _ in range(n2+1)] for __ in range(n1+1)]. That's size (n1+1)(n2+1)(k1+1). Similarly for dpH. Since n1,n2 up to 100, k up to 10, memory ~ (101101*11)*2 ≈ 224k integers, each int ~28 bytes in Python, ~6MB, acceptable.
But we might want to reduce memory by using dictionaries or only current i? But not needed.
Alternatively, we can combine dpF and dpH into one DP with dimension for last type and run length. But anyway.
We must be careful about indexing: l ranges from 1 to k1 inclusive, and for dpF we only use up to k1. When we add footman and l+1 <= k1, we update dpF[i+1][j][l+1]. For dpH, l up to k2.
Now we should confirm that we didn't double count transitions when we set dpF[1][0][1] and dpH[0][1][1] and then later process those states. That is correct.
Now consider edge cases: If k1=0? But constraints say >=1, so not.
But there is an alternative: the DP could be done in order of total length, but our loop is fine.
Now implement solve().
Pseudo-code:
def solve():
import sys
MOD = 100000000
n1, n2, k1, k2 = map(int, sys.stdin.read().split())
# allocate dpF and dpH
dpF = [[[0](k1+1) for _ in range(n2+1)] for __ in range(n1+1)]
dpH = [[[0](k2+1) for _ in range(n2+1)] for __ in range(n1+1)]
if n1 > 0:
dpF[1][0][1] = 1
if n2 > 0:
dpH[0][1][1] = 1
for i in range(n1+1):
for j in range(n2+1):
# process dpF
for l in range(1, k1+1):
val = dpF[i][j][l]
if val == 0:
continue
# add footman
if i < n1 and l < k1:
dpF[i+1][j][l+1] = (dpF[i+1][j][l+1] + val) % MOD
# add horseman
if j < n2:
dpH[i][j+1][1] = (dpH[i][j+1][1] + val) % MOD
# process dpH
for l in range(1, k2+1):
val = dpH[i][j][l]
if val == 0:
continue
# add horseman
if j < n2 and l < k2:
dpH[i][j+1][l+1] = (dpH[i][j+1][l+1] + val) % MOD
# add footman
if i < n1:
dpF[i+1][j][1] = (dpF[i+1][j][1] + val) % MOD
ans = 0
for l in range(1, k1+1):
ans = (ans + dpF[n1][n2][l]) % MOD
for l in range(1, k2+1):
ans = (ans + dpH[n1][n2][l]) % MOD
print(ans)
Now test with samples.
We'll test quickly mentally.
Now consider potential off-by-one: when we add footman from dpH, we set dpF[i+1][j][1] because new run length for footman becomes 1. That's correct.
When we add horseman from dpF, we set dpH[i][j+1][1].
Now ensure we handle case where n1=0 or n2=0 properly. In allocation, we have indices from 0 to n1, and we use i < n1 condition. If n1=0, then dpF[1][0][1] is out of range because n1=0, so we skip. Also the loops for i from 0 to n1, i will be 0 only. For i=0, dpF[0][j] are all zeros because we never set any. The dpH[0][1][1] set only if n2>0. Then transitions: from dpH states, adding footman requires i < n1, but i=0 and n1=0, so false; adding horseman allowed if j < n2 and l < k2. So we can only build sequences of H. At the end, answer will sum dpF[0][n2] (but i=0, not n1? Actually n1=0, so final state is (0, n2). For dpF[0][n2]... but dpF is defined for i=0 and j=n2, but we never set any dpF at i=0? However, we sum dpF[n1][n2] which is dpF[0][n2]; dpF[0][n2][l] likely zero because we never set footmen. So answer is sum dpH[0][n2][l] only. That's correct. So code works even with n1=0.
Similarly for n2=0.
Thus the conditions n1>0 and n2>0 in initialization are necessary to avoid index out of range. So we must check.
In Python, if we allocate dpF with size n1+1, then index n1+1 is out of range only if we attempt to set dpF[1][0] when n1=0. So we guard with if n1>0. Similarly for dpH.
Now verify that dpF and dpH allocation with n1=0 and n2=0? Then dpF size 1, dpH size 1? But constraints say n1,n2 >=1, so not needed. But safe.
Now we can think about possible optimization or alternative DP.
But we should ensure that the DP works even if k1 > n1? That's fine. The inner loops over l up to k1, but dpF[i][j][l] might be zero for l > i obviously, but we still loop up to k1. That's fine.
Now we can consider that the DP might also be done with fewer dimensions by using dp[i][j][c] where c is the length of last run, but we already have that.
Now consider if the answer might be large but modulo 1e8.
Now test another random case: n1=3,n2=3,k1=2,k2=2. Count manually? Not needed.
Now also note that the DP transitions are essentially a simple automaton.
Now we can produce final answer.
Now double-check modulo: 100000000 is 10^8. The problem says modulo 100000000 (10^8). So we mod.
Now there might be alternative ways to compute using combinatorics: maybe DP with prefix sums. But our DP is enough.
Now we might implement with memory optimization: use only two layers of i? Not necessary.
Now test with extreme values: n1=100,n2=100,k1=10,k2=10. DP loops: 10110110 ≈ 102k per inner l loops, and each inner does constant. So overall operations ~ 2101101102? Actually for each state we may do up to 2 updates, but it's fine.
Now ensure we don't use recursion depth.
Now think about possible integer overflow in Python? No.
Now finalize.
But I'd like to double-check the DP logic with some other small cases:
Case: n1=1,n2=1,k1=1,k2=1. Possible sequences: 12 and 21 => 2. Our DP: base: dpF[1][0][1]=1, dpH[0][1][1]=1.
Process:
i=1,j=0: dpF: i=1,n1=1 so i<1 false, cannot add F. j<1? j=0<1 => add H => dpH[1][1][1] +=1.
i=0,j=1: dpH: j<1? false; i<1? true => add F => dpF[1][1][1] +=1.
Now i=1,j=1: dpF[1][1][1]=1, dpH[1][1][1]=1.
dpF: i<1 false, j<1 false => none.
dpH: j<1 false, i<1 false => none.
Answer: sum dpF[1][1]=1 + sum dpH[1][1]=1 => 2. Good.
Case: n1=1,n2=2,k1=1,k2=2. Sequences of 1 F and 2 H, with F isolated (k1=1) and H runs <=2. All arrangements: total permutations: 3!/(2!1!)=3 sequences: 122, 212, 221. Check constraints: In 122, H run length 2 at end? Actually 1 2 2: H run length 2 allowed; no two F. So valid. 212: H's separated, run lengths 1 each; valid. 221: H run length 2 at start, valid. So 3. DP should output 3.
Let's test quickly:
Base: dpF[1][0][1]=1, dpH[0][1][1]=1.
i=0,j=1: dpH[0][1][1]=1: j<2? yes and l<2? l=1<2 => dpH[0][2][2] +=1; i<1? yes => dpF[1][1][1] +=1.
i=1,j=0: dpF[1][0][1]=1: i<1? false; j<2? yes => dpH[1][1][1] +=1.
Now i=1,j=1: have dpF[1][1][1]=1, dpH[1][1][1]=1 (from i=1,j=0), and maybe other dpH? also from i=0,j=1 we had dpH[0][2][2] and dpF[1][1][1]. So after i=0,j=1, we have dpF[1][1][1]=1, dpH[0][2][2]=1. Now i=1,j=1, we also have dpH[1][1][1]=1 from i=1,j=0. Also we may have dpH[1][1][?] from others? Not yet.
Process i=1,j=1:
dpF[1][1][1]: i<1 false; j<2 true => dpH[1][2][1] +=1.
dpH[1][1][1]: j<2 true and l<2 true => dpH[1][2][2] +=1; i<1 false.
Now i=0,j=2: dpH[0][2][2]=1:
j<2? false; i<1? true => dpF[1][2][1] +=1.
Now i=1,j=2: states: dpH[1][2][1] from earlier? Actually from dpF[1][1][1] we added dpH[1][2][1] =1; from dpH[1][1][1] we added dpH[1][2][2]=1; also dpF[1][2][1]=1 from i=0,j=2. Also maybe other.
Process i=1,j=2:
dpF[1][2][1]: i<1 false; j<2 false => none.
dpH[1][2][1]: j<2 false; i<1 false => none.
dpH[1][2][2]: j<2 false; i<1 false => none.
Now final answer: sum dpF[1][2][l] = dpF[1][2][1] =1; sum dpH[1][2][l] = dpH[1][2][1] + dpH[1][2][2] = 1+1 =2; total 3. Good.
Thus DP correct.
Now we must note that the problem's output modulo 10^8, but we have been modding each addition. That's fine.
Now we should consider that the DP might overflow in Python if we don't mod often? But we mod each update, so fine.
Now another nuance: The problem says "strictly more than k1 footmen standing successively" so the condition is that the length of consecutive footmen must be <= k1. So we enforce that we cannot add a footman if current run length l equals k1 because then l+1 would be >k1. So condition l < k1 is correct. Similarly for horsemen.
Now we must ensure that our initial runs of length 1 are always <= k1, k2. Since k1,k2 >=1, fine.
Now we can finalize.
We can also compress DP by using 2D arrays with two layers for current run length? But not needed.
Now let's think about memory allocation in Python: we have dpF = [[[0]*(k1+1) for _ in range(n2+1)] for __ in range(n1+1)]. That's okay. But for large n1,n2=100, k1=10, that's 101 * 101 * 11 ≈ 112211, each integer 28 bytes = ~3 MB per array, 6 MB total, okay.
Now check for time: Python loops: about 10010010* something = 100k * 2 = 200k operations, plus overhead. Should be fine.
But we have nested loops: for i in range(n1+1): for j in range(n2+1): for l in range(1,k1+1): That is O(n1n2k1). For k1 up to 10, n1=100,n2=100 -> 1e5, similarly for k2. So total ~2e5 loops, each with a couple of if checks and assignments. That's trivial.
Now we need to handle input: line with four integers.
Now we also need to output answer.
Now we can write code.
But I also want to consider whether there's any tricky case: for example, when k1 > n1 but also > something, DP still works.
Now confirm with a test: n1=5,n2=5,k1=10,k2=10, essentially no run constraints because k1 and k2 are larger than total counts. Then number of beautiful arrangements = total arrangements = C(10,5) = 252. But also if runs can exceed 10? Actually if both k1>=5 and k2>=5, no run can exceed 5 because total counts are 5 each. So all arrangements allowed. So answer = 252 mod 1e8 = 252. Let's test DP mentally? Should work.
Now edge: k1=1,k2=1 with equal counts: n1=n2=3. Then only alternating sequences possible: either starting with 1: 121212? Actually length 6, with 3 of each: alternating starting with 1 gives: 1 2 1 2 1 2. That's 3 ones, 3 twos. Starting with 2: 2 1 2 1 2 1. So two sequences. DP should give 2.
Check: Our DP will count. Good.
Now check n1=3,n2=3,k1=2,k2=2. That's more interesting. Possibly many sequences. But trust DP.
Now we might need to consider modulo at final sum, but we already mod during updates.
Now finalize solution.
But we should also think about alternative: DP with dp[i][j][c] where c is 0/1 and we also need run length. Could also be done using inclusion-exclusion? Not needed.
Thus solution ready.
Now I'll produce final answer with explanation.
But the user asked: "Gaius Julius Caesar, a famous general, loved to line up his soldiers. ..." So they expect code.
But as ChatGPT, I'm to provide solution description? Possibly the user wants the answer in code? The prompt says: "Input ... Output ..." It's a problem statement. So they likely want the solution programmatically? But we are to respond with final answer? Usually in such interactions, we are to produce the solution code or approach. The user hasn't explicitly said "write code". But the context: "Gaius Julius Caesar ..." with sample I/O. It's a competitive programming problem. The user likely wants the solution. Since ChatGPT can't execute code, but we can give explanation and final code.
Given the instruction: "You are an AI assistant. Provide answer in final." Possibly they want the final answer, i.e., the number of beautiful arrangements for given input? But no specific input provided; they gave examples. So the request is to solve the problem generically. They want to "Find the number of beautiful arrangements" given input, so they want a program or algorithm.
Thus I'll present an explanation and then provide Python code.
But the instruction "Provide answer in final." might mean I should produce the final output for a given input? The conversation: The user gave the problem statement and examples. Probably they want the solution approach. However, there is no specific input to compute; they want the general solution. So I'll produce a description and code.
Given the note: "You are an AI assistant. Provide answer in final." So I think I should give a concise final answer, perhaps the algorithm or the code.
To be safe, I'll give both explanation and code.
Now produce final answer.
I'll include the DP approach with states (i, j, last, len) and mod 1e8.
Now finalize. We can solve the problem using dynamic programming. Let dpF[i][j][l] be the number of ways to arrange i footmen and j horsemen such that the last soldier is a footman and the current run of footmen at the end has length l (1 ≤ l ≤ k1). Similarly, dpH[i][j][l] is defined for horsemen with run length l (1 ≤ l ≤ k2). Transitions:
Start with dpF[1][0][1] = 1 (if n1>0) and dpH[0][1][1] = 1 (if n2>0). After filling the table, the answer is the sum over l of dpF[n1][n2][l] and dpH[n1][n2][l], modulo 100000000.
Complexities: time O(n1·n2·max(k1,k2)), memory O(n1·n2·max(k1,k2)). The constraints (n1,n2 ≤ 100, k1,k2 ≤ 10) make this efficient.
Below is an implementation in Python:
import sys
MOD = 100000000
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n1, n2, k1, k2 = map(int, data)
# dpF[i][j][l] : last is footman, run length l (1..k1)
dpF = [[[0] * (k1 + 1) for _ in range(n2 + 1)] for __ in range(n1 + 1)]
# dpH[i][j][l] : last is horseman, run length l (1..k2)
dpH = [[[0] * (k2 + 1) for _ in range(n2 + 1)] for __ in range(n1 + 1)]
if n1 > 0:
dpF[1][0][1] = 1
if n2 > 0:
dpH[0][1][1] = 1
for i in range(n1 + 1):
for j in range(n2 + 1):
# from footman endings
for l in range(1, k1 + 1):
val = dpF[i][j][l]
if val == 0:
continue
# add another footman
if i < n1 and l < k1:
dpF[i+1][j][l+1] = (dpF[i+1][j][l+1] + val) % MOD
# add a horseman
if j < n2:
dpH[i][j+1][1] = (dpH[i][j+1][1] + val) % MOD
# from horseman endings
for l in range(1, k2 + 1):
val = dpH[i][j][l]
if val == 0:
continue
# add another horseman
if j < n2 and l < k2:
dpH[i][j+1][l+1] = (dpH[i][j+1][l+1] + val) % MOD
# add a footman
if i < n1:
dpF[i+1][j][1] = (dpF[i+1][j][1] + val) % MOD
ans = 0
for l in range(1, k1 + 1):
ans = (ans + dpF[n1][n2][l]) % MOD
for l in range(1, k2 + 1):
ans = (ans + dpH[n1][n2][l]) % MOD
print(ans)
if __name__ == "__main__":
solve()