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 }

    真神奇啊

  • 相关阅读:
    ubuntu---系统备份
    Linux---基础篇学习记录
    ubuntu---如何进入高级选项
    ubuntu---【nvidia-smi】命令参数含义
    ubuntu---卡顿,强制关机,引发的一系列问题
    CUDA---安装提示“Not enough space on parition mounted at /”
    C++ ---usleep()功能
    ubuntu---常用命令
    yolo---训练时,命令行中加入-map 和 -dont_show
    jQuery火箭图标返回顶部代码
  • 原文地址:https://www.cnblogs.com/luomi/p/5958202.html
Copyright © 2011-2022 走看看