zoukankan      html  css  js  c++  java
  • lightoj-1050

    1050 - Marbles
    PDF (English) Statistics Forum
    Time Limit: 2 second(s) Memory Limit: 32 MB
    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[R][B]作为状态方程,通过观察发现,但R>=B时,dp[R][B]肯定是为0的

    当R<B时 我们可以发现dp[R][B] = R/(R+b)*dp[R-1][B-1] + B/(R+B)*dp[R][B-2];

    根据这两个状态预处理好dp方程就可以了;

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    double dp[600][600];
    
    void init(){
        for(int i=1;i<510;i++) dp[0][i] = 1;
        for(int i=1;i<510;i++){
            for(int j=i+1;j<510;j++){
                dp[i][j] = (double)i*1.0/(i+j)*dp[i-1][j-1] + (double)j*1.0/(i+j)*dp[i][j-2];
            }
        }
        return ;
        
    }
    
    int main(){
        init();
        int T,r,b;
        scanf("%d",&T);
        for(int t=1;t<=T;t++){
            
            scanf("%d%d",&r,&b);
            printf("Case %d: %.6lf
    ",t,dp[r][b]);
        }
        
    }
  • 相关阅读:
    为什么要使用href=”javascript:void(0);”
    切图要点
    css属性学习
    CentOS7使用yum安装RabbitMQ
    Linux下,root权限才能启动1024以下端口的程序
    Gitlab备份和恢复操作记录
    rpm批量卸载所有带有Java的文件
    如何使用微软官方工具制作win10启动盘
    重装系统时,将MBR分区转为GPT 分区
    walle 2.0 上线部署
  • 原文地址:https://www.cnblogs.com/yuanshixingdan/p/5565609.html
Copyright © 2011-2022 走看看