zoukankan      html  css  js  c++  java
  • lightoj1050_概率dp

    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
    ");
    42         else
    43             printf("%.6f
    ", f[r][b]);
    44     }
    45     return 0;
    46 }

    真神奇啊

  • 相关阅读:
    理解FreeRTOS的任务状态机制
    stm32【按键处理:单击、连击、长按】
    stm32f4单片机 硬件浮点运算
    stm32 HAL库 串口无法接收数据的问题
    Single Sign On —— 简介(转)
    关于第三方库安装时很慢或者读取超时问题处理
    设计模式》状态机模式
    设计模式》责任链模式
    设计模式》访问者模式
    设计模式》策略者模式
  • 原文地址:https://www.cnblogs.com/luomi/p/5958202.html
Copyright © 2011-2022 走看看