Stone Game
Time Limit: | 1000MS | Memory Limit: | 10000K |
Description
Dennis and Joyce love to collect stones and play games with them. Up till now they have collected Na white stones and Nb black stones, and they are going to play a new game. The rules of the game is as follows:
(1) All the stones are put into a pocket initially, so that each stone has equal possibility to be picked up, and each time a stone is picked up randomly.
(2) The two players take turns to pick up a stone, if the stone is black, the current player should put it back to the pocket; otherwise the player could keep the stone.
(3) Whoever picks up the last white stone wins the game.
Now they haven’t decided who plays first. To make a better decision, Dennis have asked you, an excellent programmer, to calculate the winning possibility for the first player. Since they are going to start playing soon, you must finish the job as quickly as possible, Come on!
Input
The first line is a single number n, the number of test cases followed. In the next n lines, each line contains two non-negative integers, Na and Nb,(0≤Na, Nb≤64).
Output
For each test case, output a real number indicating the possibility for the first player to win. The answer should be rounded to 6 digits after the decimal point. No extra spaces are allowed.
Sample Input
3
1 0
0 1
3 10
Sample Output
1.000000
0.000000
0.500282
题目大意:一个袋子有na个白石子和nb个石子,两人轮流从袋子中取石子。若取到黑石子则重新放回袋子,若取到白石子则取出。取到最后一个白石子为胜者。
解题思路:设win[i]为剩下i个白石子时取的人赢的概率。则有加法原理,等于取到白石子的概率*(1-win[i-1])+取到黑石子的概率*(1-win[i])。
win[i]=[i/(i+nb)]*(1-win[i-1])+[nb/(i+nb)]*(1-win[i])。
则有递推式:
win[i]={1-[i/(i+nb)*win[i-1]]}/[1+nb/(i+nb)];
win[0]=0;
则问题为求win[na];
代码略。
摘自《国际大学生程序设计竞赛例题解(五)》