zoukankan      html  css  js  c++  java
  • LightOJ 1042

    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
    5
    1 2
    2 3
    2 5
    11 6
    4 11
    
    Output for Sample Input
    Case 1: 0.3333333333
    Case 2: 0.13333333
    Case 3: 0.2285714286
    Case 4: 0
    Case 5: 0.1218337218
    

    记忆化搜索,模拟选取过程,每次选取只有两种情况

    • 1.第一个人选红色,第二个人选蓝色
    • 2.第一个人选蓝色,第二个人选蓝色

    所以对于r个红球和b个篮球, 有(r/(r+b))的概率是第一种情况, 有(b/(r+b))的概率是第二种情况。

    记忆化搜索代码如下:

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 1e9+7;
    
    double dp[507][507];
    
    double dfs(int r, int b)
    {
        if(dp[r][b] >= 0)
            return dp[r][b];
    
        if(r == 0)
            return dp[r][b] = 1;
    
        if(r > 0 && b == 0)
            return dp[r][b] = 0;
    
        double ans = 0;
        if(r >= 1)
            ans += 1.0*r/(r+b) * dfs(r-1, b-1);
        if(b >= 2)
            ans += 1.0*b/(r+b) * dfs(r, b-2);
        return dp[r][b] = ans;
    }
    
    void solve(int cases)
    {
        int r, b;
        scanf("%d%d", &r, &b);
        printf("Case %d: %.8f
    ", cases, dfs(r, b));
    }
    
    int main()
    {
        memset(dp, -1, sizeof(dp));
    
        int t;
        scanf("%d", &t);
        for(int i=1; i<=t; ++ i)
            solve(i);
        return 0;
    }
    
    

    动态规划代码如下:

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 1e9+7;
    
    double dp[507][507];
    
    void solve(int cases)
    {
        int r, b;
        scanf("%d%d", &r, &b);
        printf("Case %d: %.8f
    ", cases, dp[r][b]);
    }
    
    int main()
    {
        memset(dp, 0, sizeof(dp));
        for(int i=0; i<=500; ++ i)
            dp[0][i] = 1;
        for(int i=1; i<=500; ++ i) // 因为 (i+j) 为奇数,所以不用考虑 i == 1 && j == 1 的情况
        {
            for(int j=2; j<=500; ++ j)
            {
                dp[i][j] = 1.0 * i / (i + j) * dp[i-1][j-1] + 1.0 * j / (i + j) * dp[i][j-2];
            }
        }
        int t;
        scanf("%d", &t);
        for(int i=1; i<=t; ++ i)
            solve(i);
        return 0;
    }
    
    
  • 相关阅读:
    CodeceptJS学习笔记-高级用法03-tag
    CodeceptJS学习笔记-高级用法02-数据驱动测试
    CodeceptJS学习笔记-高级用法01-多次运行片状测试
    CodeceptJS学习笔记-入门05-生成HTML测试报告
    CodeceptJS学习笔记-入门04-CodeceptUI 安装和启动
    CodeceptJS学习笔记-入门03-使用nodejs运行
    CodeceptJS学习笔记-入门02
    Java线上问题排查
    rabbitmq分布式事务解决方案
    Java架构技术体系
  • 原文地址:https://www.cnblogs.com/aiterator/p/6744168.html
Copyright © 2011-2022 走看看