lightoj1050_概率dp

本文介绍了一个游戏挑战中的概率计算问题,玩家需要计算在一个含有红蓝两种颜色弹珠的袋子里,根据给定的红蓝弹珠数量,求出玩家获胜的概率。文章提供了具体的实现代码,并考虑了边界条件和优化策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://lightoj.com/volume_showproblem.php?problem=1050

Your friend Jim has challenged you to a game. He has a bag containing red and blue marbles. There will be an odd number of marbles in the bag, and you go first. On your turn, you reach into the bag and remove a random marble from the bag; each marble may be selected with equal probability. After your turn is over, Jim will reach into the bag and remove a blue marble; if there is no blue marble for Jim to remove, then he wins. If the final marble removed from the bag is blue (by you or Jim), you will win. Otherwise, Jim wins.

Given the number of red and blue marbles in the bag, determine the probability that you win the game.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case begins with two integers R and B denoting the number of red and blue marbles respectively. You can assume that 0 ≤ R, B ≤ 500 and R+B is odd.

Output

For each case of input you have to print the case number and your winning probability. Errors less than 10-6 will be ignored.

Sample Input

Output for Sample Input

5

1 2

2 3

2 5

11 6

4 11

Case 1: 0.3333333333

Case 2: 0.13333333

Case 3: 0.2285714286

Case 4: 0

Case 5: 0.1218337218

思路:定义dp[i][j] 为 袋子中有i个红球和j个红球时获胜的概率

那么根据题意我只可以任意拿而对手只拿蓝球。那么

dp[i][j] = (拿到红球的概率) * dp[i-1][j-1] + (拿到蓝球的概率) * dp[i][j-2];

边界:当红球没有时,获胜的概率为1

注意点:T比较大,需要把所有数据预处理出来直接查询,否则超时

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 #include <vector>
 7 #include <ctime>
 8 #include <queue>
 9 #include <list>
10 #include <set>
11 #include <map>
12 using namespace std;
13 #define INF 0x3f3f3f3f
14 typedef long long LL;
15 
16 double f[505][505];
17 void init()
18 {
19     memset(f, 0, sizeof(f));
20     for(int i = 1; i <= 500; i++)
21         f[0][i] = 1;
22     f[0][1] = 1;
23     for(int i = 1; i <= 500; i++)
24     {
25         for(int j = 2; j <= 500; j++)
26         {
27             f[i][j] = i*1.0 / (i+j) * f[i-1][j-1] + j*1.0 / (i+j) * f[i][j-2];
28         }
29     }
30 }
31 int main()
32 {
33     int t, r, b;
34     init();
35     scanf("%d", &t);
36     for(int ca = 1; ca <= t; ca++)
37     {
38         scanf("%d %d", &r, &b);
39         printf("Case %d: ", ca);
40         if(b < r)
41             printf("0\n");
42         else
43             printf("%.6f\n", f[r][b]);
44     }
45     return 0;
46 }

真神奇啊

 

转载于:https://www.cnblogs.com/luomi/p/5958202.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值