LightOJ 1265 Island of Survival

来源:互联网 发布:起点听书软件 编辑:程序博客网 时间:2024/05/17 06:51

题目:

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

Sample Output

Case 1: 1

Case 2: 0

Case 3: 0.3333333333

Case 4: 1

这个题目其实可以理解为排序。

把所有的动物排序,然后每次取前2个,取出来之后的操作就和题目一样,如果动物没死,放回原处。

下次又取最前面2个动物。。。直到结束。

如果t为奇数,自然是人死。

如果t为偶数,当且仅当所有的老虎都排在人的前面的时候,人活。

这样问题可以进一步抽象,t(偶数)个老虎和1个人,随机排序,求人排在最后的概率(别说你不会微笑

代码:

#include<iostream>#include<iomanip>using namespace std;int main(){int T, t, d;cin >> T;for (int cas = 0; cas < T; cas++){cin >> t >> d;t++;cout << "Case " << cas + 1 << ": " << fixed << setprecision(10) << (t % 2)*1.0 / t << endl;}return 0;}


2 0
原创粉丝点击