Vote (Large)
Time limit5sMemory limit512 MB
Given N supporters of A and M of B in random arrival order, find the probability A leads after every vote; a ballot-problem computation.
- Level
Medium5 of 10
- Topics
- Combinatorics, Probability, Math, Dynamic programming
- Solved
- No attempts yet
Problem
Candidates A and B are the only two people running in an election. Polling already tells us that exactly voters support A and exactly voters support B. Since is greater than , A wins.
Voters arrive at the polling place one at a time. Their order is chosen uniformly at random among all possible orders. After each vote is cast, the poll worker updates the running count and writes down which candidate is ahead so far. If the two counts are equal, no candidate is ahead.
Compute the probability that A stays ahead the whole time, meaning that A is ahead right after every single vote.
Input
The first line contains the number of test cases . Each of the next lines contains two integers and , the number of voters supporting A and the number supporting B.
Limits
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from 1 and is the probability that A is ahead right after every vote.
Print with exactly eight digits after the decimal point. If the exact value does not stop at the eighth decimal digit, round it at that digit, and round up when the remainder falls exactly halfway. For example, a probability of prints as 0.00195313.
Explanation
Take and . There are three voters, two of whom support A. Call those two A1 and A2. The six possible arrival orders are A1 A2 B, A2 A1 B, A1 B A2, A2 B A1, B A1 A2, B A2 A1. Only the first two keep A ahead right after every vote. With the order A1 B A2, A is ahead after the first vote but tied after the second. So the answer is .
Take and . There is a single voter and that voter supports A. Only one order is possible, and A is ahead after that one vote.