1. 程式人生 > >Island of Survival LightOJ

Island of Survival LightOJ

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

題意:

如果你和老虎相遇,老虎一定會殺了你。 如果老虎和鹿相遇,老虎會吃掉鹿。 如果兩隻鹿相遇,什麼也不會發生。 如果你遇到鹿,你可能會殺了鹿,也可能不會。 如果兩隻老虎相遇,它們會打架到死。所以,兩者都會被殺死。

每兩個動物(包括自己)相遇的概率是相同的,問你的最大存活率

題解:因為問的是我的存活概率,所以該題就與鹿無關,簡單概率dp,看程式碼

//#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 =  1e2+5;
const double PI  =  3.141592653589;
const double E   =  2.718281828459;


double dp[maxn][maxn];

//Think:因為人只能被老虎吃掉,所以最終的答案與鹿無關,只要判斷老虎的數量就行,如果沒有老虎答案為1,如果老虎的數量是奇數
//那麼人一定會被吃掉,所以答案是0,如果老虎的數量是偶數,那麼計算兩隻老虎相遇的結果,兩個老虎相遇的種類數
//t*(t-1)/2,P=t*(t-1)/2/((t+1)*t/2),整理的P=(t-1)/(t+1)
//


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;
}