F - Island of Survival LightOJ - 1265

LightOJ - 1265 

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
//如果老虎的数量是奇数,答案是0
//如果老虎的数量是偶数,则计算两只老虎相遇的结果,两个老虎相遇的概率=t*(t-1)/2 / ((t+1)*t/2)(不考虑鹿)

看代码

//#include<bits/stdc++.h>
//#include <unordered_map>
//#include<unordered_set>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
#include<climits>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
#include<string>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
const int INF  =  0x3f3f3f3f;
const int O    =  1e5;
const int mod  =  1e9+7;
const int maxn =  1e3+5;
const double PI  =  3.141592653589;
const double E   =  2.718281828459;


double dp[maxn][maxn];

int main()
{
    int T;scanf("%d",&T);
    int l = 0;
    while(T--)
    {
        int n, m;
        scanf("%d%d",&n,&m);
        MT(dp,0);
        for(int i=0;i<=m;i++) dp[1][i] = 0,dp[0][i] = 1;
        
        for(int i=2;i<=n;i++) for(int j=0;j<=m;j++)
            dp[i][j] = (double)i * (i-1) / (i+1) / i * dp[i-2][j];
        
        printf("Case %d: %.7f\n",++l,dp[n][m]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mannix_Y/article/details/82655662