Island of Survival 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 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
Sample Output
Case 1: 1
Case 2: 0
Case 3: 0.3333333333
Case 4: 1

大致题意:在一个岛上有t只老虎,d只羊,还有你,每天都会有两个动物相遇,如果是你碰到老虎,你死;如果老虎碰到羊,羊死;如果两只老虎相遇,则一起死;如果你碰到羊,你可以选择杀或不杀。如果某一天老虎全死完了,则你获胜。现在问你获胜的最大概率期望是多少。

一开始想用dp写结果没想出来,后来看了别人的博客后才发现自己想多了orz。
正解思路:因为在整个决策过程中,鹿的存在与否并不会对最后的结果产生影响,why?Becase决定人最后存活与否的最关键的因素在于老虎们是否全部死亡,而鹿并不会让人或者老虎数量发生变化,且让老虎死亡的唯一可能就是在某天有两只老虎碰面了,而且老虎如果死亡则一定是成双成对地死(<_<),所以我们计算人最后存活的概率则等价于计算老虎在碰见人之前就全部gg的概率。
所以结论就很明显了,当老虎是奇数时,人一定会死,当老虎数量为0时,则人一定存活,而当老虎的数量为非零偶数时,设有t只老虎,无视掉鹿参与碰面的情况,则在前t/2个有老虎参与碰面的日子里(因为有t只老虎,所以第t/2+1天时只会剩下一只虎或一个人)选取t个生物参与碰面,若不区分老虎,则总情况数为C(t+1,t)=C(t+1,1)=t+1,而人能存活的情况就是选取的t个生物为全部的t只老虎,情况数为C(t,t)=1,则人最后能够存活的概率便是(能够存活的情况)/(总情况)=1/(t+1),这道题到这儿才算是完美解出.
所参考的博客
http://blog.csdn.net/omrailgun/article/details/54092306
代码如下

#include<bits/stdc++.h>
const double eps=1e-7;
#define LL long long int
using namespace std;
int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        int t,d;
        double ans=1;
        scanf("%d%d",&t,&d);
        if(t%2)
            ans=0;
        else 
        {
            while(t)
            {
                ans*=1.0*(t-1)/(t+1);
                t-=2;
            }
        }
        printf("Case %d: %.10lf\n",cas,ans);    
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值