PRoblem
A and B are the only two candidates competing in a certain election. We know from polls that exactly N voters support A, and exactly M voters support B. We also know that N is greater than M, so A will win.
Voters will show up at the polling place one at a time, in an order chosen uniformly at random from all possible (N + M)! orders. After each voter casts their vote, the polling place worker will update the results and note which candidate (if any) is winning so far. (If the votes are tied, neither candidate is considered to be winning.)
What is the probability that A stays in the lead the entire time – that is, that A will always be winning after every vote?
Input
The input starts with one line containing one integer T, which is the number of test cases. Each test case consists of one line with two integers N and M: the numbers of voters supporting A and B, respectively.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the probability that A will always be winning after every vote.
y will be considered correct if y is within an absolute or relative error of 10-6 of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.
Limits
1 ≤ T ≤ 100. Small dataset
0 ≤ M < N ≤ 10. Large dataset
0 ≤ M < N ≤ 2000.
解釋:
當(dāng)要求dp[i][j]時(shí):假設(shè)第(i+j)個(gè)人去投票,他可能投給A, 也可能投給B,因?yàn)橐阎衖個(gè)人投給了A,j個(gè)人投給了B,則最后這一票有i/(i+j)的概率投給了A,有j/(i+j)的概率投給了B。當(dāng)最后一票是投給A的時(shí)候,之前A一直勝的概率就為dp[i - 1][j];當(dāng)最后一票是投給B的時(shí)候,之前A一直勝的概率就為dp[i][j - 1]。合起來即為dp[i][j]。最后只要直接查找相應(yīng)的位置即可得到每個(gè)測(cè)試用例的結(jié)果。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注