Compute the attacker win probability in a Risk battle with D-sided dice where the defender picks one or two dice after seeing the attack roll.
Medium7Dynamic programmingProbabilityGame theoryCombinatoricsNo attempts yetTime limit2sMemory limit256 MBVladimir and Mark play the strategy board game Risk. Players occupy regions of the world and try to conquer whole continents. Mark holds a region with M armies and wants to attack an adjacent region that Vladimir holds with N armies. Mark is the weaker player, so he asks you for the probability that he wins that battle.
The battle follows the standard Risk rules, with one change: the dice do not have to be six sided. Every die has D sides with D≥1, the sides carry different values, and each side comes up with the same probability.
A battle is a sequence of rounds. Suppose the attacker has A armies in the region he attacks from and the defender has B armies in his region. One round runs like this.
The battle ends when the attacker has only one army left, and then the defender has won, or when the defender has no army left, and then the attacker has won.
For example, the attacker rolls three dice showing 4, 2 and 1 while the defender rolls two dice showing 3 and 2. The first comparison is 4 against 3, which the attacker wins. The second comparison is 2 against 2, which the defender wins. Both players lose one army. For another example, if the two highest dice of the attacker both show the largest value D, the defender is better off rolling a single die, because then he loses at most one army.
Vladimir knows the rules and the dice, and he defends so that Mark's probability of winning the battle is as small as possible.
The first line contains an integer T, the number of test cases. Each test case is given on two lines.
For each test case print one line with the probability that Mark wins the battle, rounded to exactly six digits after the decimal point. Do not use scientific notation.