「Light OJ1265」Island of Survival【概率dp】

1265 - Island of Survival

Time Limit: 2 second(s) Memory Limit: 32 MB

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

  • If you and a tiger meet, the tiger will surely kill you.
  • If a tiger and a deer meet, the tiger will eat the deer.
  • If two deer meet, nothing happens.
  • If you meet a deer, you may or may not kill the deer (depends on you).
  • 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 ) T (\leq 200) T(200), denoting the number of test cases.

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

Output

For each case, print the case number and the expected probability. Errors less than 1 0 − 6 10^{-6} 106 will be ignored.

Sample Input

4
0 0
1 7
2 0
0 10

Output for Sample Input

Case 1: 1
Case 2: 0
Case 3: 0.3333333333
Case 4: 1

题意

  • 开始小岛上有 t t t只老虎, d d d只鹿,还有你,每天会等概率的有两个生物相遇,相遇的情况如下,当一定不会发生人被吃的情况时,游戏结束,求人存活的最大概率
    • 鹿与鹿相遇,不发生任何事
    • 鹿与虎相遇,虎吃掉鹿
    • 虎与虎相遇,两只老虎同归于尽
    • 虎与人相遇,虎吃掉人
    • 鹿与人相遇,人可以选择吃或者不吃鹿

题解

  • 为什么还会是最大概率呢?因为在第五种情况中,人可以有两种选择
  • 当老虎为 0 0 0时,不论鹿为多少,存活概率为 1 1 1
  • d p [ i ] [ j ] dp[i][j] dp[i][j]表示老虎 i i i只,鹿 j j j只的情况下,人存活的最大概率,则对于第五种情况分类讨论
    • 吃掉鹿,列出方程为
      d p [ i ] [ j ] = i × j C i + j + 1 2 d p [ i ] [ j − 1 ] + C i 2 C i + j + 1 2 d p [ i − 2 ] [ j ] + C j 2 C i + j + 1 2 d p [ i ] [ j ] + j C i + j + 1 2 d p [ i ] [ j − 1 ] dp[i][j]=\frac{i\times j}{C_{i+j+1}^{2}}dp[i][j-1]+ \frac {C_i^2}{C_{i+j+1}^{2}}dp[i-2][j]+\frac{C_j^2}{C_{i+j+1}^{2}}dp[i][j]+\frac{j}{C_{i+j+1}^{2}}dp[i][j-1] dp[i][j]=Ci+j+12i×jdp[i][j1]+Ci+j+12Ci2dp[i2][j]+Ci+j+12Cj2dp[i][j]+Ci+j+12jdp[i][j1]
      右边 d p [ i ] [ j ] dp[i][j] dp[i][j]移到左边然后化简
    • 不吃鹿,列出方程为
      d p [ i ] [ j ] = i × j C i + j + 1 2 d p [ i ] [ j − 1 ] + C i 2 C i + j + 1 2 d p [ i − 2 ] [ j ] + C j 2 C i + j + 1 2 d p [ i ] [ j ] + j C i + j + 1 2 d p [ i ] [ j ] dp[i][j]=\frac{i\times j}{C_{i+j+1}^{2}}dp[i][j-1]+ \frac {C_i^2}{C_{i+j+1}^{2}}dp[i-2][j]+\frac{C_j^2}{C_{i+j+1}^{2}}dp[i][j]+\frac{j}{C_{i+j+1}^{2}}dp[i][j] dp[i][j]=Ci+j+12i×jdp[i][j1]+Ci+j+12Ci2dp[i2][j]+Ci+j+12Cj2dp[i][j]+Ci+j+12jdp[i][j]
      只是最后一项稍作变化
    • 上面两种情况求 d p [ i ] [ j ] dp[i][j] dp[i][j]后取最大值即可

代码

#include<bits/stdc++.h>

using namespace std;

int get(int n)
{
    return n*(n-1)/2;
}
double dp[1005][1005];

int main()
{
    int t;scanf("%d",&t);
    for(int i=0;i<=1000;i++) dp[0][i]=1.0;
    for(int i=1;i<=1000;i++){
        for(int j=0;j<=1000;j++){
            double sum=0,rest=0;
            if(i>=2) sum+=get(i)*dp[i-2][j];
            if(j>=1) sum+=(i*j*dp[i][j-1]),rest=j*dp[i][j-1];
            dp[i][j]=max(sum/(get(i+j+1)-get(j)-j),(sum+rest)/(get(i+j+1)-get(j)));
        }
    }
    for(int cas=1,m,n;cas<=t;cas++){
        scanf("%d %d",&m,&n);
        printf("Case %d: %.10lf\n",cas,dp[m][n]);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值