| <p> | |
| Two chess grandmasters, Andrew and Jacob, are having an epic chess showdown to determine which of them is the uncontested best player in the world! | |
| </p> | |
| <p> | |
| The showdown consists of <strong>N</strong> games. In each game, one player plays as White and the other plays as Black. In the first game, Andrew plays as White. | |
| After each game, the player who loses it chooses which color they'll play as in the following game. | |
| However, the victor of the final game wins the entire showdown, regardless of the results of the previous games! | |
| </p> | |
| <p> | |
| In each game, each player may decide to attempt to win or attempt to lose: | |
| </p> | |
| <p> | |
| <ol> | |
| <li> | |
| If both players play to win, then Andrew wins with probability <strong>W<sub>w</sub></strong> if he plays as White | |
| (and loses with probability 1 - <strong>W<sub>w</sub></strong>, as there are no draws at this high level of play). | |
| Similarly, he wins with probability <strong>W<sub>b</sub></strong> if he plays as Black. | |
| </li> | |
| <li> | |
| If both players play to lose (achieved by tipping over their own king as quickly as possible), then Andrew loses with probability | |
| <strong>L<sub>w</sub></strong> if he plays as White, and loses with probability <strong>L<sub>b</sub></strong> if he plays as Black. | |
| </li> | |
| <li> | |
| If exactly one player wants to win a game, then he's guaranteed to win it. | |
| </li> | |
| </ol> | |
| </p> | |
| <p> | |
| Assuming both players play optimally in an attempt to win the showdown, what is Andrew's probability of besting Jacob? | |
| </p> | |
| <h3>Input</h3> | |
| <p> | |
| Input begins with an integer <strong>T</strong>, the number of showdowns between Andrew and Jacob. | |
| For each showdown, there is first a line containing the integer <strong>N</strong>, then a line containing the space-separated values | |
| <strong>W<sub>w</sub></strong> and <strong>W<sub>b</sub></strong>, then a line containing the space-separated values | |
| <strong>L<sub>w</sub></strong> and <strong>L<sub>b</sub></strong>. These probabilities are given with at most 9 decimal places. | |
| </p> | |
| <h3>Output</h3> | |
| <p> | |
| For the <strong>i</strong>th showdown, print a line containing "Case #<strong>i</strong>: " followed by | |
| the probability that Andrew wins the entire showdown. Your output should have at most 10<sup>-6</sup> absolute or relative error. | |
| </p> | |
| <h3>Constraints</h3> | |
| <p> | |
| 1 ≤ <strong>T</strong> ≤ 10,000 <br /> | |
| 1 ≤ <strong>N</strong> ≤ 1,000,000,000 <br /> | |
| 0 ≤ <strong>W<sub>w</sub></strong>, | |
| <strong>W<sub>b</sub></strong>, | |
| <strong>L<sub>w</sub></strong>, | |
| <strong>L<sub>b</sub></strong> ≤ 1 <br> | |
| </p> | |
| <h3>Explanation of Sample</h3> | |
| <p> | |
| In the first showdown, Andrew plays White and wins the only game with probability 0.9. | |
| In the second showdown, Jacob will throw the first game to force Andrew to play Black in the second game. Jacob can guarantee a loss in the first game, and Andrew will win the second game with probability 0.8. | |
| </p> | |