We need to compute probability that the princess wins.
We have w white, b black initially. Princess draws first. Then dragon draws (if game hasn't ended). After dragon draws, an extra mouse jumps out (scared). This jumping out occurs after the dragon's draw, and it's not considered a draw; it just reduces the population. The princess draws carefully, so no jump after her draws. The game continues until someone draws a white mouse (win for the drawer). If bag becomes empty and no one drew white, dragon wins.
We need probability princess wins given optimal? No, it's pure probabilistic process.
We can model this as a Markov process on state (w, b) representing number of white and black mice in bag at start of princess's turn? Let's define state as (w,b) before a turn? Actually careful: At the beginning, princess will draw. Then after her draw, if she draws white, she wins immediately; else (draws black) the bag decreases by 1 black. Then it's dragon's turn. Dragon draws from remaining (w, b-1). If dragon draws white, he wins immediately; else (draws black) the bag decreases by 1 black (so now b-2). Then after dragon's draw (and if he didn't win because he drew black), a mouse jumps out: one mouse from the remaining (w, b-2) randomly jumps out. So we need to consider probabilities of which color jumps out. Then the state becomes (w', b') after these steps, and it's princess's turn again.
If at any turn, the drawer draws white, game ends with that player winning. If bag becomes empty before someone draws white, dragon wins (since it says: If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Also note that after dragon's draw and jump, it's princess's turn, but if bag is empty, then princess can't draw, so dragon wins? Actually the condition says: "If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins." This can happen at start of a turn (princess or dragon) if bag empty. But since princess draws first, if initial w=0 and b=0? But w,b >=0. If both zero, princess draws? There are no mice, so according to rule, dragon wins. So probability princess =0.
Thus we need to compute DP. Since w,b up to 1000, state space is about 1e6, which is fine. But transitions involve probabilities that depend on draws and jump.
Define f(w,b) = probability that princess eventually wins starting from state (w,b) at the beginning of princess's turn (i.e., it's princess's turn to draw). We need f(w,b). Base cases:
-
If w=0: There are no white mice. So princess can't draw a white. She draws a black (if any). Then after her draw, if any mice left? Actually she draws from bag. If w=0 and b>0, she will draw a black (probability 1). Then state for dragon's turn: (0, b-1). Dragon will draw from that. If still no white, he draws black (if b-1>0). If after dragon's draw, maybe bag becomes empty. But we can compute with recursion. However we can also base on: if w=0, then the only chance for princess to win is if dragon never draws white (impossible because no white) and eventually bag empties? But bag empty leads to dragon win. So actually princess cannot win if there are no white mice at all, because the only way to win is to draw a white mouse. However, could she win if the white mouse appears? No, w=0 means no whites. So she can't win. So f(0,b) = 0 for any b? But careful: Could there be a scenario where the white mouse appears later? No, because w=0, no white mice ever. So indeed f(0,b)=0. But wait: What about the possibility that a white mouse jumps out? No, only existing mice. So if initial w=0, no white ever. So princess can't win. So f(0,b)=0.
-
If w>0 and b=0: Then only white mice. Princess draws first: she draws white with probability 1, wins immediately. So f(w,0)=1 for w>0.
-
If w=0 and b=0: empty bag, dragon wins => f(0,0)=0.
Now for general w>0, b>0.
We need to consider princess's turn:
Princess draws a mouse from (w,b). Probabilities:
- Draw white: probability w/(w+b). She wins immediately => contributes w/(w+b).
- Draw black: probability b/(w+b). Then state after her draw: (w, b-1) and it's dragon's turn.
Now we need to compute, from the state after princess draws black (i.e., (w, b-1) and it's dragon's turn), the eventual probability that princess wins. Let's define g(w,b) as probability that princess wins given that it is now dragon's turn and the bag contains w white and b black mice.
So after princess draws black, we go to g(w, b-1). Note: b>=1 so b-1 >=0.
Thus f(w,b) = w/(w+b) + (b/(w+b)) * g(w, b-1). (1)
Now we need g(w,b) for states where it's dragon's turn, and there are w white, b black mice.
Dragon draws from (w,b). Cases:
- Dragon draws white: probability w/(w+b). Then dragon wins immediately, princess loses => contribution 0.
- Dragon draws black: probability b/(w+b). Then after dragon draws black, the bag becomes (w, b-1). But then an additional mouse jumps out (scared). This happens only if there are still mice left after dragon's draw? The problem: "After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice)." So after dragon draws, regardless of whether he drew white? Actually if he draws white, the game ends, so jumping out is irrelevant. But if he draws black, the game continues. Then one mouse from the remaining (after draw) jumps out. The remaining after draw: total mice = w + (b-1) = w+b-1. One mouse jumps out, chosen uniformly from these. After that, it's princess's turn on the resulting state (w', b') where w' = w - (if a white jumps out? maybe), b' = (b-1) - (if black jumps out? maybe). The jump occurs even if the bag is empty? After dragon draws black, there is at least one mouse? If w=0,b=1: after princess draw black we had (0,0)? Actually that scenario would not happen because if w=0,b=1 at start of dragon's turn, dragon draws black with probability 1, then after draw bag becomes (0,0). Then there is no mouse to jump out? The problem says "the rest of mice in the bag panic, and one of them jumps out of the bag itself". If there are no mice left, then maybe none jumps? Probably then the jump does not occur because there are no mice. But it says "the rest of mice in the bag panic, and one of them jumps out". That implies there is at least one mouse to jump? Actually if after dragon's draw the bag is empty, there are no mice to jump, so maybe nothing happens. But let's read: "Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one." So it's a random jump from the remaining mice. If there are no remaining mice, then no jump occurs. So we should handle that case: if after dragon's draw the bag becomes empty (i.e., w=0 and b-1=0), then there is no mouse to jump, and the game proceeds to princess's turn with empty bag. But then according to the rule, if bag empty and nobody has drawn white, dragon wins. Actually bag empty before princess's turn; then when it's princess's turn and bag empty, she cannot draw, so dragon wins. So from that state, princess's win probability =0. So we must incorporate that.
Also if after dragon's draw there is exactly one mouse left, then that mouse jumps out, resulting in empty bag before princess's turn. So princess loses.
Thus g(w,b) = probability that princess wins given dragon's turn with state (w,b). Write:
Let total = w+b.
- Dragon draws white: probability w/total => princess loses => contribution 0.
- Dragon draws black: probability b/total. Then after draw, state = (w, b-1). Now we need to consider the jump.
Let after-draw state: w' = w, b' = b-1. Let total_rem = w' + b' = w+b-1.
If total_rem == 0: then after dragon's draw, bag is empty, no jump (or jump irrelevant). Then it's princess's turn with empty bag -> princess cannot win => probability 0.
If total_rem > 0: then a random mouse jumps out uniformly from the total_rem mice.
So with probability (w' / total_rem) a white jumps out, resulting in state (w-1, b') for princess's turn? Actually careful: w' = w, b' = b-1. If a white jumps, new white count = w - 1, black count = b' = b-1. If a black jumps, new white count = w, black count = b-1 - 1 = b-2.
After the jump, it's princess's turn. So the probability princess eventually wins from that state is f(white_remaining, black_remaining).
Thus, conditional on dragon drawing black, we have:
If total_rem == 0: contribute 0.
Else: princess win probability = (w' / total_rem) * f(w-1, b') + (b' / total_rem) * f(w, b'-1?) Actually b' is b-1; if black jumps, new black = b' - 1 = b-2. So state = (w, b-2). So second term: (b' / total_rem) * f(w, b-2).
But note: b' could be 0, in which case black jump probability is 0, fine.
Now, we also need to consider the possibility that after dragon draws black, the bag still has mice but the jump might remove the last mouse? That's automatically accounted: if total_rem = 1, then after jump, state becomes empty, and f(0,0)=0. So it's okay.
Thus we can write:
g(w,b) = (b/(w+b)) * ( if w+b-1 == 0 then 0 else ( (w)/(w+b-1) * f(w-1, b-1) + (b-1)/(w+b-1) * f(w, b-2) ) ).
But careful: The after-draw state has w white, b-1 black. So the probability of white jump is w/(w+b-1) (if w+b-1>0). Then resulting state for princess: (w-1, b-1). But if w=0, then white jump probability = 0, okay. Similarly, black jump results in (w, b-2). Note that if b-1=0, then black jump probability = 0.
Thus:
g(w,b) = (b/(w+b)) * J(w,b) where
J(w,b) =
if (w+b-1 == 0): 0
else: (w/(w+b-1))*f(w-1, b-1) + ((b-1)/(w+b-1))*f(w, b-2)
Now we have recurrence for f and g. But we can combine to eliminate g: Express f directly using f of smaller states. However, careful: f(w,b) uses g(w,b-1). So plugging g:
f(w,b) = w/(w+b) + (b/(w+b)) * g(w, b-1).
Now compute g(w, b-1):
g(w, b-1) = ( (b-1)/(w+b-1) ) * J(w, b-1) where J(w, b-1) depends on state after dragon draws black from (w, b-1). But careful: When it's dragon's turn with (w, b-1), after he draws black, the remaining after draw is (w, (b-1)-1 = b-2) with total w+b-2. Then a jump occurs if w+b-2 > 0, etc.
Thus:
g(w, b-1) = (b-1)/(w+b-1) * ( if w+b-2 == 0 then 0 else ( w/(w+b-2) * f(w-1, b-2) + (b-2)/(w+b-2) * f(w, b-3) ) ).
Wait careful: In J(w, b-1), after dragon draws black from (w, b-1): initial white w, black = b-1.
- Draw black probability (b-1)/(w+b-1). That's the factor outside J. Inside J: after drawing black, state becomes (w, (b-1)-1 = b-2). So remaining total = w + (b-2) = w+b-2. If w+b-2 == 0: then J=0. Else jump: white jump prob = w/(w+b-2), resulting state (w-1, b-2); black jump prob = (b-2)/(w+b-2), resulting state (w, b-3). So:
g(w, b-1) = (b-1)/(w+b-1) * ( if w+b-2 == 0 then 0 else ( w/(w+b-2) * f(w-1, b-2) + (b-2)/(w+b-2) * f(w, b-3) ) ).
Thus:
f(w,b) = w/(w+b) + (b/(w+b)) * [ (b-1)/(w+b-1) * ( if w+b-2 == 0 then 0 else ( w/(w+b-2) * f(w-1, b-2) + (b-2)/(w+b-2) * f(w, b-3) ) ) ].
But this seems messy and might involve f with b-3 etc. However we can instead compute f and g via DP iteratively over states.
Given constraints up to 1000, we can have DP table f[w][b] for w,b from 0 to 1000. Similarly g[w][b] but we can compute g from f on the fly, but to compute f we need g for (w,b-1) which might require f for (w-1,b-2) and (w,b-3) which are smaller in total mice (since total decreases). So we can compute DP in increasing order of total mice? Yes. Because f(w,b) depends on g(w,b-1) which depends on f with total reduced by at least 2 (since after two draws and a jump). Actually total in f(w,b) is T = w+b. The recurrences: f(w,b) depends on g(w,b-1) which involves f of states with total T-2 or T-3? Let's examine: f(w,b) needs g(w,b-1). g(w,b-1) involves after dragon draws black and jump: states (w-1, b-2) with total (w-1)+(b-2)= w+b-3, and (w, b-3) with total w+b-3. Also possible the case where after dragon's draw total_rem = 0 leads to immediate 0, which doesn't require deeper f. So f(w,b) depends on f values with total <= w+b-3 (or possibly w+b-2? Actually if w+b-2 == 0, then we don't need f; else we need f for total = w+b-3). So we can compute f for states in order of increasing total. Also base cases f(w,0) =1 for w>0, f(0,b)=0 for b>=0, and f(0,0)=0.
We also need g? We can compute f directly using recurrence with g as intermediate, but we can also incorporate g into f recursion. However we may just compute both DP arrays. Since we need f only, we can compute f via recursion with memoization. But DP iterative may be easier: for total from 0 to w_max+b_max, compute f for all w,b with that total. But careful: f depends on g which depends on f with smaller total; we can compute g from f for smaller total.
Thus we can define arrays f[0..w][0..b] and g[0..w][0..b]. We'll fill them for increasing total.
But we also need to compute g(w,b) for total T, which depends on f for states with total T-1? Actually after dragon's turn, we jump and then it's princess's turn: the resulting state after jump has total = (w + b - 2) (since one draw and one jump reduces by 2, except when after draw total_rem=0, then we have empty bag). Let's analyze: g(w,b) is defined when it's dragon's turn. It depends on f for states with total = (w + (b-1) -1?) Let's compute: after dragon draws black, we have (w, b-1) total = w+b-1. Then a jump reduces by 1: total = w+b-2. So the states after jump have total = w+b-2, i.e., one less than previous total after draw? Actually initial total T = w+b. After dragon draws black, we remove one black, so total becomes T-1. Then a jump removes one mouse, so total becomes T-2. So the f states needed have total T-2 (unless T-2 = 0, which is base). So g(w,b) depends on f for total = T-2. So to compute g for total T, we need f for total T-2. So if we compute f for total up to T-2 before g for total T, we can compute g. Then f for total T depends on g for total T-1? Actually f(w,b) has total T, and uses g(w,b-1) which has total T-1 (since b-1 reduces total by 1). So f(T) depends on g(T-1). And g(T-1) depends on f(T-3). So f(T) depends on f(T-3) etc. So we can compute in increasing total order: for each total t from 0 to W+B (max 2000?), we can compute f and g for states with that total? But note f(w,b) uses g(w,b-1) which has total t-1, so if we compute g for all states with total t-1 before computing f for total t, that works. And g(w,b) of total t uses f for total t-2, which should already be computed if we go increasing total. So we can fill DP by increasing total.
Thus algorithm:
Let maxW = w, maxB = b given. But we need to compute for all possible states up to these? Actually we only need f(w,b) for the given w,b. But recurrence uses f of smaller w,b values. So we can compute DP for all w' <= w, b' <= b. Since w,b up to 1000, that's about 1e6 states, which is fine.
We'll store f as double array (maybe long double for precision). We'll also store g as double. We'll compute base cases:
For all w from 0..W, b from 0..B:
- f[w][0] = 1 if w>0 else 0 (since if w=0,b=0, f[0][0]=0).
- f[0][b] = 0 for b>=1? Actually if w=0,b>0, princess can't ever win because no white. But is it possible that princess could win if dragon draws black and then a white jumps? No because w=0 so no white ever. So yes f[0][b] = 0 for b>0. And f[0][0]=0.
But careful: If w=0,b>0, princess draws black, then it's dragon's turn. Could dragon cause a white? No. So princess will never draw white. So indeed f=0.
But is there any scenario where princess wins because dragon draws white? That would be dragon winning. So no.
Thus base: f[0][b] = 0 for all b >=0 (including b=0). f[w][0] = 1 for w>0.
Now we need to compute for w>0,b>0. We'll iterate total from 1 to W+B? But base covers many. We'll do nested loops for w from 0..W, b from 0..B, but we can compute in order of increasing total to ensure dependencies. Simpler: we can iterate b from 0..B, for each w from 0..W? But careful dependencies: f(w,b) needs g(w,b-1) which in turn needs f(w-1,b-2) and f(w,b-3). Those have lower b and possibly lower w. So we could compute in increasing b, and for each b, increasing w? Let's test: For a given b, f(w,b) uses g(w,b-1). g(w,b-1) depends on f(w-1,b-2) and f(w,b-3). So if we compute for b sequentially from 0 upwards, and for each b, compute w from 0..W, then by the time we get to b, we should have already computed f for lower b values (b-1, b-2, b-3) and for w and w-1 etc. So that's fine. Also we need g for (w,b-1), which we can compute on the fly using already computed f for lower b. But we might compute g as needed; but we could also precompute g for all states. Either way, we can compute f and g in nested loops with b from 0..B, w from 0..W. However need to ensure that for g(w,b) we use f for states with b-2, b-1 etc. Since b is increasing, b-2 is smaller, so f already computed. So order by b then w works.
But also we need to handle cases where indices become negative. We'll guard with conditions.
Thus we can compute:
Initialize f[0..W][0..B] with 0.
Set for w=1..W: f[w][0] = 1.
Set for b=0..B: f[0][b] = 0 (already default 0).
Set f[0][0]=0.
Now for w from 1..W:
for b from 1..B:
Compute f[w][b].
First compute term1 = w / (w+b) (as double).
Then compute g_w_bminus1 = probability from state (w, b-1) with dragon's turn. We'll need to compute that.
So compute g(w, b-1) using formula:
If b-1 == 0? Actually g(w, b-1) defined for state (w, b-1). But if b-1 = 0, then total = w + 0 = w. If w>0, it's dragon's turn with only white mice. Dragon draws white with probability 1, wins immediately => princess loses => g(w,0)=0. If w=0, then g(0,0)=0 anyway. So we can treat g(w,0)=0 for any w (since if w>0, dragon draws white and wins; if w=0, no white, dragon draws black? Actually if w=0,b=0, empty, but dragon's turn? That state can't occur because if it's dragon's turn, there must have been some mice? But we could define g(0,0)=0 as base. But we only need g(w,b-1) when b>=1, so b-1>=0. If b-1 =0, we have g(w,0)=0. So we can compute accordingly.)
Thus g(w, b-1) =
if w + (b-1) == 0: 0
else:
let total = w + (b-1)
prob_dragon_draw_black = (b-1) / total
if prob_dragon_draw_black == 0: then g=0 (because dragon draws white with prob 1? Actually if b-1=0, then prob_dragon_draw_black = 0, so g = 0, consistent). So we can compute only when b-1 >0? But we must also handle when b-1 =0, then it's zero. So we can use condition.
If b-1 == 0: g = 0.
Else:
total = w + b-1
prob_black_draw = (b-1)/total
after_draw_white = w
after_draw_black = (b-1)-1 = b-2
total_rem = w + (b-2) = w + b - 2
if total_rem == 0:
J = 0
else:
prob_white_jump = w / total_rem
prob_black_jump = (b-2) / total_rem
J = prob_white_jump * f[w-1][b-2] + prob_black_jump * f[w][b-3] (taking care indices >=0; if w=0 then prob_white_jump=0 and term zero; if b-2=0 then prob_black_jump=0 and term zero; if b-3 <0 treat as 0? Actually if b-2>=0 but b-3 negative? If b-2 >0 then b-3 could be -1? Wait b-2 is the count of black after draw before jump. Then if a black jumps, we subtract one more, so black becomes b-3. But if b-2=0, then black jump probability is 0, so term not needed. If b-2>0, then b-3 could be 0 or positive. It can't be negative because b-3 >= 0 if b>=3. If b=2, then after draw black count b-2 =0, so no black jump. So b-3 negative never occurs when black jump probability >0. So we can safely use f[w][b-3] only if b-3 >=0, else 0. But if b-2>0 and b-3 >=0, so okay. Actually check: b=2, after draw: b-1=1, dragon draws black? b-1=1, so there is one black. After drawing black, black count becomes 0, total_rem = w+0 = w. If w>0, then a jump occurs: white jump prob = w/w=1, so black jump prob = 0. So b-3 is -1, but its coefficient is 0 anyway because (b-2)=0. So we can compute J as:
J = (w / total_rem) * f[w-1][b-2] + ((b-2) / total_rem) * f[w][b-3] // if indices valid, else 0
But we need to handle cases where w-1 < 0? That only when w=0, but then prob_white_jump=0. So safe.
Thus g = prob_black_draw * J.
Now after computing g(w,b-1), we have f[w][b] = term1 + (b/(w+b)) * g(w,b-1).
But careful: The term (b/(w+b)) is the probability princess draws black, and then we go to dragon's turn. So indeed.
That seems correct.
But we must also consider the possibility that after princess draws black, the bag state (w, b-1) might have total >0? Yes, it will. But what if after princess draws black, bag becomes empty? That would require w=0 and b=1. Then state (w,b-1) = (0,0). But then it's dragon's turn with empty bag. According to rule: if bag empty and nobody has drawn white, dragon wins. So from dragon's turn with (0,0), probability princess wins = 0. So g(0,0)=0. That's consistent with our formula: if b-1=0 and w=0, total=0, g=0. So okay.
Now we need to compute all f up to given (W,B). We'll use double arrays.
Potential pitfalls: The DP might involve f[w][b-3] for b-3 negative? As argued, when computing J, the coefficient for black jump is (b-2)/total_rem. If b-2 >0, then b-3 >=0 because b-2 >=1 implies b >=3 => b-3 >=0. So no negative index. For b=2: b-2=0, coefficient 0, so we can ignore f[w][b-3] (which would be f[w][-1]). So we should code conditionally: if b-2>0, include term, else not.
Now we need to be careful about precision: Use double with 64-bit. Should be fine.
Check small examples:
Example1: w=1, b=3. Let's compute manually.
Our DP:
Initialize f[1][0] = 1; f[0][*]=0.
Compute f[1][1], f[1][2], f[1][3], etc.
We'll compute step by step.
First, for w=1,b=1:
term1 = 1/(2) = 0.5.
Need g(1,0). Since b-1=0, g=0. So f[1][1] = 0.5 + (1/2)*0 = 0.5.
But is that correct? Let's think: w=1,b=1. Princess draws first. Probability she draws white = 0.5 -> win. If she draws black (0.5), then state (1,0) dragon's turn. Dragon draws from (1,0): draws white with prob 1, wins. So princess loses. So probability =0.5. Good.
Now f[1][2]:
w=1,b=2, total=3.
term1 = 1/3 ≈ 0.3333333.
Now need g(1,1). (w=1,b-1=1)
Compute g(1,1):
total = 1+1=2.
prob_dragon_draw_black = (b-1)/total = 1/2 =0.5.
after draw: white=1, black= (b-1)-1 = 0? Actually b-1=1, after drawing black, black becomes 0, so state (1,0). total_rem = 1+0=1 >0.
white jump prob = w / total_rem = 1/1=1.
black jump prob = (b-2)/total_rem = ( -? b-2 = -1? Actually b-2 = -1? Wait b=1, b-2 = -1, but since b-1=1, after draw black count = 0, so b-2 is not defined? Actually we used (b-2) which is (1-2) = -1. But in J we used (b-2) as the count of black after draw? Let's be careful: After dragon draws black from (w,b) where b=1? Actually we are computing g(1,1): w=1, b=1. In g(w,b), b is number of black when it's dragon's turn. So for g(1,1): total = 2. Dragon draws black with prob b/total = 1/2. After drawing black, black count becomes b-1 = 0, white remains w=1. So after draw state: (1,0) with total 1. Then a jump occurs: from the remaining mice (1 white, 0 black), a random mouse jumps. Probability white jump = 1/1 =1, black jump = 0/1 =0. So resulting state: if white jumps, then (0,0); black jumps impossible. So state becomes (0,0). So princess's turn with empty bag => f(0,0)=0. So J = 1*0 + 0 =0. So g(1,1) = (1/2) * 0 = 0.
Thus g(1,1)=0. So f[1][2] = 1/3 + (2/3)*0 = 0.3333333. So princess winning probability = 1/3 ≈ 0.3333333.
But we need to verify: w=1,b=2. Let's compute manually:
Princess draws first: prob white = 1/3 -> win.
If draws black (2/3): state (1,1) dragon's turn.
Dragon draws: from (1,1). He draws white with prob 1/2 -> dragon wins. Draws black with prob 1/2 -> then after draw, state becomes (1,0), then a mouse jumps: only white, jumps out, resulting empty bag. Princess turn, bag empty => dragon wins. So in all cases after princess draws black, dragon wins. So princess total = 1/3. That matches.
Now f[1][3]:
w=1,b=3. total=4.
term1 = 1/4 = 0.25.
Need g(1,2) (since b-1=2). Compute g(1,2):
w=1,b=2 (dragon's turn). total = 3.
prob_dragon_draw_black = b/total = 2/3.
After drawing black: state becomes (w, b-1) = (1,1). total_rem = 1+1=2.
Now jump: from remaining (1,1). Probability white jump = 1/2, black jump = 1/2.
If white jumps: state becomes (0,1) -> princess's turn with (0 white, 1 black). f(0,1)=0 (since no whites). If black jumps: state becomes (1,0) -> f(1,0)=1.
So J = (1/2)*0 + (1/2)*1 = 0.5.
Thus g(1,2) = (2/3) * 0.5 = 1/3 ≈ 0.3333333.
Now f[1][3] = 1/4 + (3/4)*g(1,2) = 0.25 + 0.75 * (1/3) = 0.25 + 0.25 = 0.5. So f[1][3]=0.5. That matches sample output.
Now second sample: w=5,b=5. Output ~0.658730159. We'll later test our DP.
Thus the recurrence seems correct.
Now we need to implement DP for w up to 1000, b up to 1000. That's 1e6 states. Each state requires computing g for (w, b-1) which involves some calculations. We could compute g on the fly using f values that are already computed (since they involve lower total). But if we compute in nested loops b from 0..B, w from 0..W, we might recompute g many times. To avoid recomputation, we could precompute g array as well. But it's fine to compute g on the fly using formula; that's O(1) per state. However we need to access f[w-1][b-2] and f[w][b-3] for g(w,b-1). Those indices are within bounds if we computed all f for lower b already. Since we compute b increasing, b-2 and b-3 are smaller than current b, so they should be already computed. Also w-1 is smaller w but for a given b we compute w from 0..W? If we compute w in increasing order, w-1 is also already computed. So safe.
Thus algorithm:
Initialize f as 2D array of size (W+1) x (B+1) with 0.0.
for w in 1..W: f[w][0] = 1.0
for b in 0..B: f[0][b] = 0.0 (already set)
Now for b from 1 to B:
for w from 1 to W:
compute f[w][b] as described.
But careful: There is also dependency on f[w][b] for cases where w=0 or b=0 already set. So we can loop w from 1..W, b from 1..B.
Now inside loop:
total = w + b
term1 = w / total
// compute g_w_bminus1 = probability princess wins from state (w, b-1) on dragon's turn.
Let bw = b-1
if bw == 0: // then b-1 ==0
g = 0.0
else:
total_dragon = w + bw // = w + b -1
prob_black_draw = bw / total_dragon
// after dragon draws black: state (w, bw-1) = (w, b-2)
total_rem = w + (bw-1) // = w + b - 2
if total_rem == 0:
J = 0.0
else:
prob_white_jump = w / total_rem // if w>0 else 0
prob_black_jump = (bw-1) / total_rem // bw-1 = b-2, if b-2>=0 else 0 but total_rem>0 ensures b-2>=0? Actually if total_rem>0 and w could be 0, then bw-1 might be >0. But we should ensure we don't access negative indices.
// compute contributions:
J = 0.0
if w > 0: // but prob_white_jump may be >0 only if w>0
J += prob_white_jump * f[w-1][b-2] // need to ensure b-2 >=0; if b-2 <0, prob_white_jump is 0 anyway because w>0? Actually if b-2 <0, then total_rem = w + (b-2). If b-2 negative, that's impossible because bw-1 = b-2 must be non-negative for total_rem to be >=0. But we have total_rem>0, and b-2 could be negative only if w >0 and b=1? Let's check: b=1, bw=0, we already handled bw==0. So b>=2 when we get here. So b-2 >=0. So safe.
if (bw-1) > 0: // i.e., b-2 > 0
J += prob_black_jump * f[w][b-3] // b-3 >=0? If b=2, then bw-1 =0, not >0, so skip. If b=3, then b-3=0, which is okay (f[w][0] is defined). So we need to allow b-3=0. So condition b-2 > 0 ensures b>=3, then b-3>=0. Actually if b=2, b-2=0, skip. If b>=3, b-3>=0. So we can simply use if b-2 >= 0? But b-2>=0 is always true when we are here (since b>=2). But we must ensure we don't use f[w][negative]. So we can check if (b-2) >= 0? Actually the term prob_black_jump * f[w][b-3] requires b-3 >=0. So we can check if b-3 >= 0. But note prob_black_jump may be zero if b-2 =0. So we can compute: if (b-2) > 0 then include term because then b-3>=0; if b-2==0 then prob_black_jump=0 anyway. So we can use:
if b-2 > 0: // ensures b-3 >=0
J += prob_black_jump * f[w][b-3]
// However there is also case b-2=0 and w>0? Then prob_black_jump=0, so term not needed.
// End else
g = prob_black_draw * J
Then f[w][b] = term1 + (b / total) * g
But wait: we used b/(w+b) factor which is correct.
Double-check: In f[w][b], after princess draws black, we go to state (w, b-1) with dragon's turn. That's exactly g(w,b-1). So the factor is (b/(w+b)). Good.
Now we need to consider edge case when w>0 but b=1: then b-1=0 => g=0 => f[w][1] = w/(w+1) + (1/(w+1))*0 = w/(w+1). Let's verify: w>0,b=1. Princess draws: probability white = w/(w+1). If she draws black (prob 1/(w+1)), then state (w,0) dragon's turn. Dragon draws white with prob 1, wins. So princess wins only if she draws white. So f = w/(w+1). Correct.
Now w=1,b=2 we computed earlier f=1/3 which matches.
Now w=1,b=3 we got 0.5.
Now test w=2,b=1: f=2/(3)=0.6667. Princess draws white with prob 2/3; else draws black (1/3) goes to (2,0) dragon's turn: dragon draws white (1) wins. So f=2/3. Our formula: term1=2/3, g(2,0)=0, so f=2/3. Good.
Now test w=2,b=2: Let's compute manually to verify recurrence.
Manual: w=2,b=2.
Princess draws first: prob white = 2/4=0.5 -> win.
If draws black (0.5): state (2,1) dragon's turn.
Now dragon's turn from (2,1): total=3.
Dragon draws white with prob 2/3 -> dragon wins; draws black with prob 1/3. If draws black, state becomes (2,0), then a mouse jumps: only white mice (2). Jump: one white jumps out uniformly from the two, resulting state (1,0) (since one white left) for princess. Then princess's turn from (1,0): she draws white with prob 1 -> wins. So princess wins if princess initially draws black (0.5) AND dragon draws black (1/3) AND then after jump, princess draws white (1). So probability = 0.5 * 1/3 * 1 = 1/6 ≈ 0.1666667. Also maybe after dragon draws black and then jump, could the jump remove a black? There are no blacks left because after dragon draws black, black count becomes 0. So only white jump. So total princess win prob = 0.5 + 0.1666667 = 0.6666667. So f=2/3? Actually 0.5+0.1666667=0.6666667. Let's compute via DP.
Our DP: w=2,b=2.
term1 = 2/4=0.5.
Need g(2,1). Compute g(2,1): w=2,b=1. total=3. prob_dragon_draw_black = 1/3? Actually b=1, so prob black = 1/3. After draw black, state becomes (2,0). total_rem = 2+0=2. Jump: prob white jump = 2/2=1, black jump = 0. So resulting state (1,0) for princess. f(1,0)=1. So J = 11 = 1. g(2,1) = (1/3)1 = 1/3. Then f[2][2] = 0.5 + (2/4)(1/3) = 0.5 + 0.50.33333 = 0.5 + 0.1666667 = 0.6666667. Correct.
Now w=2,b=3: Let's compute manually maybe? We'll trust DP.
Now we need to ensure we handle the case when after dragon draws black, total_rem becomes 0. For instance, consider state (w,b) = (0,1) at dragon's turn. But if w=0, b=1, then dragon draws black? Actually dragon's turn with (0,1): total=1, dragon draws black with prob 1, after draw state becomes (0,0), total_rem=0 -> no jump, princess's turn with empty bag => princess loses. So g(0,1)=0. Our formula: b=1, bw=1? Actually w=0,b=1 -> total=1, prob_black_draw =1/1=1, after draw: (0,0), total_rem=0, so J=0, g=0. Good.
Now state (1,1) dragon's turn gave g=0 as computed earlier.
Now state (1,2) gave g = 2/3 * (?) Actually earlier we computed g(1,2)=1/3.
Now state (0,2) dragon's turn: w=0,b=2. total=2, prob_black_draw =2/2=1, after draw: (0,1), total_rem =0+1=1>0, jump: prob white jump = 0/1=0, black jump = 1/1=1 -> resulting state (0,0) -> f=0, so J=0, g=0. So g(0,2)=0. That makes sense.
Now also state (w,b) where after dragon draws black, total_rem>0 but the jump may lead to states with w=0 or b=0.
Now all good.
We must also consider scenario where after princess draws black, we go to g(w,b-1). But what if after princess draws black, the bag becomes empty? That would be when w=0,b=1. Then we compute g(0,0)=0. So f(0,1)=0? Actually w=0,b=1: f(0,1)=0 from base case. That's consistent: princess draws from (0,1), draws black with prob 1, then state (0,0) dragon's turn, dragon wins? Actually after princess draws black, bag empty? Actually (0,1) after princess draws black becomes (0,0). So it's dragon's turn with empty bag, so dragon wins. So f(0,1)=0. Good.
Now compute DP for all w,b.
Complexity O(W*B) = up to 1e6, fine.
Now we need to output f[W][B] with high precision.
Let's test sample2 with code mentally? Could approximate.
Let's implement quickly in mind: w=5,b=5.
We'll compute DP systematically maybe with a small program, but here we can attempt to compute manually with recurrence? Could derive closed form? Unlikely. We'll rely on DP.
But before coding, we need to consider if DP order matters for all states. We plan to iterate b from 0..B, w from 0..W. But we need f[w][b] for w from 0..W, but we set base for w=0 or b=0. For b from 1..B, w from 1..W, we compute f[w][b]. That uses f[w-1][b-2] and f[w][b-3] which have b-2 and b-3 < b, so already computed because b loop increases. Also w-1 <= w, and since we are iterating w from 1..W inside each b, f[w-1][b-2] is already computed because for earlier w? For b-2, we have already computed all w for that b-2. So f[w-1][b-2] is computed. Similarly f[w][b-3] is computed. So order is fine.
But careful: when b=1, f[w][1] uses g(w,0) which is 0, no further f references. Good.
When b=2, f[w][2] uses g(w,1). g(w,1) uses f[w-1][b-2? Actually for g(w,1): w, b=1? Wait we need g(w, b-1) where b-1=1. So g(w,1) is computed from f[w-1][?] and f[w][?]. For g(w,1): (w,1) dragon's turn. Since b=1, after dragon draws black we go to (w,0) and then jump etc. That uses f[w-1][-1]? Actually we need to compute g(w,1) correctly. For b=1, we handle bw==0? Actually b-1=0? Wait b=2 => b-1=1 => not zero. So we compute g(w,1). For g(w,1): w=some, b=1. total_dragon = w+1. prob_black_draw = 1/(w+1). After draw black: state (w,0). total_rem = w+0 = w. If w>0, then jump: prob_white_jump = w/w =1, so resulting state (w-1,0). f[w-1][0] =1 if w-1>0 else 0. So J = 1 * f[w-1][0]. So g(w,1) = (1/(w+1)) * f[w-1][0]. For w=1, f[0][0]=0, so g(1,1)=0. For w>1, f[w-1][0]=1, so g(w,1) = 1/(w+1). Then f[w][2] = term1 + (2/(w+2)) * g(w,1). So f[w][2] = w/(w+2) + (2/(w+2)) * (1/(w+1)) = w/(w+2) + 2/( (w+2)(w+1) ). For w=1, f=1/3 + 2/(32)=1/3+1/3=2/3? Wait 2/(32)=1/3, sum=2/3? But we computed earlier f[1][2]=1/3. Let's check: w=1,b=2 gave 1/3. Did we make mistake? For w=1,b=2, using formula: w=1 => term1=1/3. g(1,1)=? According to our formula g(w,1) = (1/(w+1))f[w-1][0]. For w=1, f[0][0]=0 => g=0. So f=1/3. Good. For w=2,b=2: term1=2/4=0.5, g(2,1)=1/(3)f[1][0]=1/31=1/3, then f=0.5 + (2/4)(1/3)=0.5+0.166667=0.666667. For w=2,b=2, that matches.
So our DP is consistent.
Now for b=3, f[w][3] will use g(w,2) which uses f[w-1][1] and f[w][0]? Actually g(w,2): w, b=2. b=2, total_dragon=w+2. prob_black_draw = 2/(w+2). after draw: state (w,1). total_rem = w+1. Jump: white jump prob = w/(w+1) -> state (w-1,1); black jump prob = 1/(w+1) (since b-2=0? Actually b=2 -> after draw black count becomes 1, so b-2? careful: In g(w,2): w, b=2. After dragon draws black, black becomes 1. So total_rem = w+1. Then black jump prob = (b-2)/total_rem? b-2=0, so black jump prob =0. So only white jump. So J = (w/(w+1))* f[w-1][1] + 0. So g(w,2) = (2/(w+2)) * (w/(w+1))*f[w-1][1]. So we need f[w-1][1] which is computed earlier (b=1). So that works.
Thus DP order is fine.
Now edge case: w=0,b>0: f[0][b]=0 base, we don't need to compute because w=0 is base. So we set f[0][b]=0 for all b. But also g(0,b) might be needed for other states? Actually f[w][b] for w>0 uses g(w,b-1) which may involve f[w-1][...] but never g(0, b-1) because w>0. However g might be called for w=0 in computing f[0][b]? But f[0][b] is base, so we don't compute via recurrence. So we don't need g(0,b). But if we compute g for completeness, we might need to compute it? Not necessary.
Now we need to be careful about floating point errors. Use double. The answer required absolute/relative error 1e-9. DP with up to 1e6 states should be fine.
Now we must check if there is any special case when b-2 < 0 etc. Already handled with conditions.
Implementation details:
We'll allocate two-dimensional arrays of size (W+1) x (B+1) as vector of vectors of double.
Initialize:
vector<vector> f(W+1, vector(B+1, 0.0));
for (int w=1; w<=W; ++w) f[w][0] = 1.0; // b=0, w>0
// f[0][b] stays 0, f[0][0]=0.
Now for b from 1 to B:
for w from 1 to W:
double total = w + b;
double term1 = w / total;
double prob = term1;
// compute g_w_bminus1
int bw = b-1;
double g = 0.0;
if (bw > 0) { // else g=0
double total_dragon = w + bw; // w + b -1
double prob_black_draw = bw / total_dragon;
// after draw black: state (w, bw-1) = (w, b-2)
int w_after = w;
int b_after = bw - 1; // = b-2
double total_rem = w_after + b_after; // w + b -2
if (total_rem > 0) {
double prob_white_jump = w_after / total_rem; // w might be 0
double prob_black_jump = b_after / total_rem; // b_after could be 0
double J = 0.0;
if (w_after > 0) {
// note: f[w_after-1][b_after] is f[w-1][b-2]
J += prob_white_jump * f[w-1][b-2];
}
if (b_after > 0) { // b_after >=1
// resulting state after black jump: (w, b_after-1) = (w, b-3)
J += prob_black_jump * f[w][b-3];
}
// if both zero, but total_rem>0 can't happen because w_after+b_after>0, so at least one >0.
g = prob_black_draw * J;
} else {
// total_rem == 0 => after dragon's draw, bag empty, no jump, princess turn on empty => f(0,0)=0 => J=0 => g=0
g = 0.0;
}
} // else g=0 if bw==0
f[w][b] = term1 + (b / total) * g;
}
But wait: In the case bw>0 but total_rem == 0, we set g=0. That occurs when after dragon draws black, the bag becomes empty. Example: w=0, bw=1? But bw>0 and w=0, b_after = bw-1 = 0, total_rem = 0+0 = 0. So g=0. That's correct.
Now we must also handle case where prob_black_draw might be zero? If bw==0, we already handled. If bw>0 but w=0? Then total_dragon = bw, prob_black_draw = 1, after draw black: state (0, bw-1). total_rem = 0 + (bw-1). If bw-1>0, then total_rem>0, and w_after=0, so prob_white_jump=0, prob_black_jump=1. So J = 1 * f[0][b_after-1] = f[0][b-3] (since b_after = b-2, b_after-1 = b-3). But f[0][anything] = 0. So J=0, g=0. If bw-1=0 (i.e., bw=1, b=2), then total_rem=0, g=0. So works.
Now also note: f[w][b] for w>0,b>0 uses f[w-1][b-2] and f[w][b-3]. For b=1, we don't need them because we skip g calculation (bw=0). For b=2, we need f[w-1][0] (since b-2=0). That's defined. For b=3, we need f[w-1][1] and f[w][0] (since b-3=0). All defined. For b=4, we need f[w-1][2] and f[w][1] which are defined because b-2=2 and b-3=1, computed earlier.
Thus DP is well-defined.
Now we just compute f[W][B] and print with high precision (e.g., printf("%.12f") or cout with setprecision(15)). The answer required absolute or relative error 1e-9, so print with at least 10-12 decimal places.
Now we must test sample2 with DP.
Let's try to compute f[5][5] manually approximate via DP:
We'll compute systematically using a table. But maybe we can compute with a quick mental program? Since it's tricky, I'll attempt to compute manually step by step using rational fractions maybe, but that would be lengthy. However, we can trust the DP. I'll later test with code.
But before finalizing, we need to consider if we can simplify or use a different approach? DP is fine.
Now note: w and b up to 1000, but DP arrays of size 1001x1001 of double: about 8 MB (8 bytes per double) = 8 million bytes ~ 8 MB, plus overhead, okay.
But we can also compress memory to 2 rows? Since f[w][b] depends on f[w-1][b-2] and f[w][b-3], which are from previous b values but not all previous rows? Actually we need f[w-1][b-2] which is one row above and two columns left, and f[w][b-3] which is same row three columns left. So we could compute with 2D array, but memory is fine.
Now let's test for some edge cases:
- w=0, b=0: answer 0. (Base)
- w=0, b>0: answer 0.
- w>0, b=0: answer 1.
- w=1000, b=1000: compute.
Potential issues: Numerical stability? The DP involves multiplication of probabilities and addition, all between 0 and 1. It should be fine.
Now let's verify another case manually: w=2,b=3. Compute using recurrence.
Compute f[2][3]:
total = 5, term1 = 2/5 = 0.4.
Need g(2,2). Compute g(2,2): w=2,b=2. total_dragon=4, prob_black_draw=2/4=0.5. After draw black: state (2,1). total_rem = 2+1=3. Jump: white jump prob = 2/3, resulting state (1,1); black jump prob = 1/3, resulting state (2,0). f(1,1) and f(2,0). f(1,1)=0.5 (as computed earlier). f(2,0)=1. So J = (2/3)*0.5 + (1/3)*1 = (1/3) + (1/3) = 2/3 ≈ 0.6666667. So g(2,2)=0.5 * (2/3)=1/3 ≈0.3333333.
Then f[2][3] = 0.4 + (3/5)* (1/3) = 0.4 + 0.2 = 0.6. So probability 0.6.
Check manually: w=2,b=3. Let's compute manually to verify:
Princess draws first: P(white)=2/5=0.4 -> win.
If draws black (0.6): state (2,2) dragon's turn.
Dragon from (2,2): total=4. Cases:
- Dragon draws white: prob 2/4=0.5 -> dragon wins.
- Dragon draws black: prob 2/4=0.5. Then after draw black, state (2,1) with 3 mice. Then a random mouse jumps:
- White jumps (prob 2/3) -> state (1,1) princess's turn.
- Black jumps (prob 1/3) -> state (2,0) princess's turn.
From (1,1): princess wins with prob f(1,1)=0.5. From (2,0): princess wins with prob 1.
So after dragon draws black, princess win prob = (2/3)*0.5 + (1/3)*1 = 1/3+1/3=2/3. So overall after princess initial black, princess win prob = 0.5 * (2/3) = 1/3 ≈0.33333. Multiply by 0.6 (prob of initial black) gives 0.2. Plus initial 0.4 = 0.6. So correct.
Now check f[3][3] maybe? But fine.
Now confirm second sample via DP manually maybe approximate:
We can compute with a quick mental spreadsheet? Could compute using rational numbers to confirm 0.658730159.
Let's attempt to compute f[5][5] using recurrence step by step with fractions to check if we get about 0.658730159.
We'll need f for all lower states up to (5,5). Let's compute systematically.
We'll use rational numbers to avoid floating errors. But manual may be long but doable.
Better to trust DP, but I want to validate the recurrence with known sample.
I'll try to compute DP table up to (5,5). We'll compute f for all w,b from 0..5.
We already have base: f[w][0]=1 for w>0; f[0][b]=0 for b>=0; f[0][0]=0.
Now compute b=1:
We need f[w][1] for w=1..5.
Formula: f[w][1] = w/(w+1) because g(w,0)=0. So:
f[1][1]=1/2=0.5
f[2][1]=2/3≈0.6666666667
f[3][1]=3/4=0.75
f[4][1]=4/5=0.8
f[5][1]=5/6≈0.8333333333
Now b=2:
We need f[w][2] for w=1..5.
Recall formula: f[w][2] = w/(w+2) + (2/(w+2)) * g(w,1)
where g(w,1) = (1/(w+1)) * f[w-1][0] (since b=1, after draw black: state (w,0) then jump: always white jump to (w-1,0), f[w-1][0]=1 if w-1>0 else 0). So g(w,1) = (1/(w+1)) * (1 if w>=2 else 0). Actually if w=1, f[0][0]=0, so g(1,1)=0. If w>=2, f[w-1][0]=1, so g(w,1) = 1/(w+1).
Thus:
For w=1: f[1][2] = 1/3 + (2/3)0 = 1/3 ≈0.33333333
For w=2: f[2][2] = 2/4 + (2/4)(1/3) = 0.5 + 0.50.33333333 = 0.5 + 0.166666667 = 0.666666667
For w=3: f[3][2] = 3/5 + (2/5)(1/4) = 0.6 + 0.40.25 = 0.6 + 0.1 = 0.7
Check: 3/5=0.6, 2/5=0.4, g(3,1)=1/4=0.25 => product =0.1, sum=0.7.
For w=4: f[4][2] = 4/6 + (2/6)(1/5) = 2/3 ≈0.6666667 + (0.33333330.2?) Let's compute exactly: 4/6 = 2/3 ≈0.6666667, factor (2/6)=1/3≈0.33333, g=1/5=0.2, product=0.0666667, sum=0.7333334. Actually 2/3=0.6666667, plus 0.0666667 = 0.7333334. But more precisely: 4/6 = 2/3, (2/6)=1/3, g=1/5 => term = 1/15 ≈0.0666666667. So f = 2/3 + 1/15 = (10/15+1/15)=11/15 ≈0.73333333.
For w=5: f[5][2] = 5/7 + (2/7)(1/6) = 5/7 ≈0.7142857 + (2/7)(1/6)= (2/42)=1/21≈0.0476190 => sum ≈0.7619047. Exactly: 5/7 = 30/42? Actually 5/7 = 30/42? 5/7 = 30/42? 5/7 = 30/42? 5/7 = (56)/(76)=30/42, plus 2/71/6 =2/42=1/21=2/42? Wait 2/7*1/6 = 2/42 = 1/21 ≈0.0476190. So sum = 32/42 = 16/21 ≈0.76190476.
So f[5][2] = 16/21 ≈0.7619047619.
Now b=3:
We need f[w][3] for w=1..5.
We have formula: f[w][3] = w/(w+3) + (3/(w+3)) * g(w,2)
Now compute g(w,2). As derived earlier: for w>=1, b=2. So g(w,2) = (2/(w+2)) * (w/(w+1)) * f[w-1][1] because only white jump. Let's derive carefully.
For state (w,2) on dragon's turn:
total = w+2.
prob_draw_black = 2/(w+2).
After draw black: state (w,1). total_rem = w+1.
Jump: only white jump (since b_after = 1-? Actually after draw, black count = 1, but jump from remaining: white jump prob = w/(w+1), black jump prob = (1)/(w+1)?? Wait careful: After dragon draws black from (w,2), black becomes 1, white remains w. So remaining mice: w white, 1 black, total = w+1. Then one mouse jumps out. The probability that a white jumps is w/(w+1). The probability that a black jumps is 1/(w+1). However earlier I said black jump prob = (b-2)/total_rem? For g(w,b), after draw, black count = b-1. For b=2, b-1=1. Then b_after = b-1? Actually we used variable b_after = b-1? Wait in our earlier general derivation: For g(w,b): after dragon draws black, we have (w, b-1). Then total_rem = w + (b-1). Then jump probabilities: white jump = w / total_rem, black jump = (b-1) / total_rem. But careful: The black count after draw is b-1, but the mouse that jumps is from the remaining mice, which includes w white and (b-1) black. So black jump probability is (b-1)/total_rem. In our earlier for g(w,2), b=2, so after draw black count = 1, so black jump prob = 1/(w+1). I earlier incorrectly said black jump prob = (b-2)/total_rem. That was a mistake. Let's check our earlier general expression: In g(w,b) we had: after draw black: state (w, b-1). Then total_rem = w + (b-1). Then white jump prob = w / total_rem, black jump prob = (b-1) / total_rem. This is correct. Then resulting state if white jumps: (w-1, b-1). If black jumps: (w, b-2). Because black count reduces by one more. So indeed after black jump, black count becomes (b-1)-1 = b-2.
Thus for g(w,2): b=2 -> after draw black: (w,1). total_rem = w+1. white jump prob = w/(w+1). black jump prob = 1/(w+1). After white jump: (w-1,1). After black jump: (w,0). So J = (w/(w+1))*f[w-1][1] + (1/(w+1))*f[w][0]. And f[w][0]=1 if w>0 else 0 (but w>=1 for g(w,2) because w>0? Actually w could be 0, but we likely won't need g(0,2) because f[0][b] is base; but if w=0, then g(0,2) we might need? Possibly not. But for w>=1, f[w][0]=1. So J = (w/(w+1))*f[w-1][1] + (1/(w+1))*1.
Thus g(w,2) = (2/(w+2)) * J.
But earlier I omitted the black jump term! In my earlier reasoning for f[2][3] I used g(2,2) and computed J = (2/3)*f[1][1] + (1/3)*f[2][0] = (2/3)*0.5 + (1/3)*1 = 2/6 + 1/3 = 1/3 + 1/3? Actually (2/3)*0.5 = 1/3, (1/3)*1 = 1/3, sum=2/3. That's what I used earlier and got correct f[2][3] = 0.6. So my earlier J included black jump term. But later when deriving formula for g(w,2) I mistakenly wrote only white jump. Let's correct: The black jump term is present. In my earlier DP pseudo-code I used: after draw, b_after = b-1, and then black jump probability = b_after / total_rem, and resulting state (w, b_after -1) = (w, b-2). That matches: b_after = b-1, so black jump leads to (w, b-2). So for b=2, b_after=1, black jump leads to (w, -1)? b-2 = 0? Actually b-2 = 0. So state (w, 0) which is fine. So in code, we used b_after = bw-1, and black jump leads to f[w][b_after-1] = f[w][b-3]? Wait careful: In general, after dragon draws black, we have (w, b-1). Then we have jump: if white jumps, state (w-1, b-1). If black jumps, state (w, (b-1)-1) = (w, b-2). So the indices: white jump -> f[w-1][b-1]; black jump -> f[w][b-2]. Not b-3! Because we are at state (w,b) for dragon's turn. After draw black, black count becomes b-