【概率期望】Island of Survival

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

题意:

  1. 如果你和老虎相遇,老虎肯定会杀了你
  2. 如果老虎和鹿相遇,老虎就会吃掉鹿
  3. 如果两只鹿相遇,什么也不会发生
  4. 如果你遇到鹿,你可能会或可能不会杀死鹿(取决于你)
  5. 如果两个老虎相遇,两个老虎都将被杀死。
每天俩动物相遇,现在让你求你预期存活的期望值:
分析:我们可以看到,人的死活与鹿的个数无关,且老虎为奇数的时候必死。
考虑老虎个数为n(偶数)的情况,最优策略是一直取老虎相遇,求最后人剩下的概率。
AC 代码:
#include <iostream>
using namespace std;
int main()
{
  int t = 0;
  cin >> t;
  for (int i = 1; i <= t; i++)
  {
    int n, m;
    cin >> n >> m;
    if (n%2==0)
    {
      double ans = 1;
      while (n != 0) {
        ans *= (n - 1)*1.0 / (n + 1);
        n -= 2;
      }
      printf("Case %d: %f\n", i, ans);
    } 
    else
    {
      printf("Case %d: %d\n", i, 0);
    }
     
  }
  
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

20 − 18 =