stone game (概率,递推)

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];


代码略。

 

 

 

摘自《国际大学生程序设计竞赛例题解(五)》

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值