概率——K - Island of Survival LightOJ - 1265

返回二级目录——kaugnbin概率dp习题集

博客目录

原题

原题传送门

You are in a reality show, and the show is way too real that they threw into an island. Only two kinds of animals are in the island, the tigers and the deer. Though unfortunate but the truth is that, each day exactly two animals meet each other. So, the outcomes are one of the following

a)      If you and a tiger meet, the tiger will surely kill you.

b)      If a tiger and a deer meet, the tiger will eat the deer.

c)      If two deer meet, nothing happens.

d)      If you meet a deer, you may or may not kill the deer (depends on you).

e)      If two tigers meet, they will fight each other till death. So, both will be killed.

If in some day you are sure that you will not be killed, you leave the island immediately and thus win the reality show. And you can assume that two animals in each day are chosen uniformly at random from the set of living creatures in the island (including you).

Now you want to find the expected probability of you winning the game. Since in outcome (d), you can make your own decision, you want to maximize the probability.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing two integers t (0 ≤ t ≤ 1000) and d (0 ≤ d ≤ 1000) where tdenotes the number of tigers and d denotes the number of deer.

Output

For each case, print the case number and the expected probability. Errors less than 10-6 will be ignored.

Sample Input

4

0 0

1 7

2 0

0 10

Sample Output

Case 1: 1

Case 2: 0

Case 3: 0.3333333333

Case 4: 1

题目大意、分析

题目大意:你被困在一个小岛上了,岛上面有两种动物虎和鹿。每天都有两只动物呆在一起,所以会出现以下几种情况

(1)你和老虎呆在一起,你就会被老虎吃掉

(2)老虎和鹿呆在一起,老虎会吃掉鹿

(3)两只鹿呆在一起,没有什么事情发生

(4)你和鹿呆在一起,你可以选择杀或者不杀鹿(决定权在你)

(5)两只老虎呆在一起,他们会互相打斗,两只都会死掉

如果有一天你确定你不会被杀死,那么你就会离开这个岛。现在给出虎和鹿的数量,求能够安全离开岛的概率。

解题思路:这是一道概率DP题,所求的是概率,所以要正向推导。定义dp[i][j]为还有i只老虎j只鹿时的逃离概率,很明显,dp[0][j] = 1,dp[i][0] = 0。其余情况分为以下几种

(1)人和鹿呆在一起,这种情况发生的概率为C(1,j) / C(2,1 + i + j) * (dp[i][j] + dp[i][j - 1]) / 2

(2)两只鹿呆在一起,这种情况发生的概率为C(2,J) / C(2,1 + i + j) * dp[i][j]

(3)虎和鹿呆在一起,这种情况发生的概率为i * j / C(2,i + j + 1) * dp[i][j - 1]

(4)两只虎呆在一起,这种情况发生的概率为C(2,i) / C(2,1 + i + j) * dp[i - 2][j]

所以dp[i][j] = C(1,j) / C(2,1 + i + j) * (dp[i][j] + dp[i][j - 1]) / 2 + C(2,J) / C(2,1 + i + j) * dp[i][j] + i * j / C(2,i + j + 1) * dp[i][j - 1] + C(2,i) / C(2,1 + i + j) * dp[i - 2][j]

将含有dp[i][j]的项移到一边即可得到关于dp[i][j]的递推式。

AC代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int T;
    int i,j;
    cin>>T;
    int t,b;
    double p=1;
    int cs=1;
    while(T--)
    {
        cin>>t>>b;
        p=1;
        if(t&1)
        {
            p=0;
        }
        else
        {
            while(t)
            {
                p=p*(t-1)/(t+1);
                t-=2;
            }
        }
        printf("Case %d: %.8f\n",cs++,p);
    }
}

猜你喜欢

转载自blog.csdn.net/GreyBtfly/article/details/82501448