LightOj:1265-Island of Survival

Island of Survival

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

Program Description

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 t denotes 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

Output for Sample Input

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


解题心得:

  1. 很有意思的一道题,如果很天真的跟着题意走,那就直接入坑了。其实,这里面的鹿根本没啥用啊
    • 从思想上来理解,人遇到了鹿对人没影响,老虎遇到了鹿对老虎没影响,最后的概率只跟老虎和人有关系,所以在遇到鹿的情况直接忽略就当什么也没发生。
    • 从数学上来理解,最后列出方程会发现,关于鹿的部分可以直接约分约掉,也是对于最后答案没有影响的。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1010;
double dp[maxn];

double get_pro(double x,double y)
{
    double x1 = y*(y-1.0);
    double x2 = x*(x-1.0);
    double ans = x2/x1;
    return ans;
}

int main()
{
    int T,cas = 1;
    scanf("%d",&T);
    while(T--)
    {
        memset(dp,0,sizeof(dp));
        int t,d;
        scanf("%d%d",&t,&d);
        if(t%2 != 0)//如果老虎是单数,那么人必死
        {
            printf("Case %d: %.8f
",cas++,0);
            continue;
        }
        for(int i=0;i<=t;i+=2)
        {
            if(i == 0)
            {
                dp[i] = 1.0;
                continue;
            }
            dp[i] = get_pro(i,i+1);
            if(i >= 2)
                dp[i] *= dp[i-2];
        }
        printf("Case %d: %.8f
",cas++,dp[t]);
    }
}
原文地址:https://www.cnblogs.com/GoldenFingers/p/9107225.html