博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lightoj1050_概率dp
阅读量:4648 次
发布时间:2019-06-09

本文共 2220 字,大约阅读时间需要 7 分钟。

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 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 using namespace std;13 #define INF 0x3f3f3f3f14 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 else43 printf("%.6f\n", f[r][b]);44 }45 return 0;46 }

真神奇啊

 

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

你可能感兴趣的文章
DP(动态规划)
查看>>
chkconfig
查看>>
2.抽取代码(BaseActivity)
查看>>
夏天过去了, 姥爷推荐几套来自smashingmagzine的超棒秋天主题壁纸
查看>>
反射的所有api
查看>>
css 定位及遮罩层小技巧
查看>>
项目中非常有用并且常见的ES6语法
查看>>
[2017.02.23] Java8 函数式编程
查看>>
sprintf 和strcpy 的差别
查看>>
JS中window.event事件使用详解
查看>>
ES6深入学习记录(一)class方法相关
查看>>
C语言对mysql数据库的操作
查看>>
INNO SETUP 获得命令行参数
查看>>
clientcontainerThrift Types
查看>>
链接全局变量再说BSS段的清理
查看>>
HTML5与CSS3权威指南之CSS3学习记录
查看>>
docker安装部署
查看>>
AVL树、splay树(伸展树)和红黑树比较
查看>>
多媒体音量条显示异常跳动
查看>>
运算符及题目(2017.1.8)
查看>>