题目链接:https://vjudge.net/problem/LightOJ-1265
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 | Output for Sample Input |
4 0 0 1 7 2 0 0 10 | Case 1: 1 Case 2: 0 Case 3: 0.3333333333 Case 4: 1 |
题意:
一座岛上有t只老虎、d只鹿和1个人。枚举随机抽取两只动物(包括人)碰面。如果鹿遇鹿则无事;如果鹿遇虎,则鹿死;如果虎遇虎,则都死;如果人遇鹿,则鹿可能死也可能不死;如果人遇虎,则人死。问人生存的概率是多少?
题解:
1.可知,人能否存活只与老虎有关。
2.当老虎的数量为奇数时,人生存的概率为0。因为老虎是成对成对死的,那么最后必定能剩下一只老虎把人吃掉。
3.当老虎的数量为偶数时,只有在选中人与虎之前,老虎成对成对地死,人才有可能存活。
4.概率 P = C[t][2]/C[t+1][2]*C[t-2][2]/C[t-1][2]……*C[2][2]/C[3][2] = (t-1)/(t+1)*(t-2)/(t-1)*…… * 2/3 。
解释:由于鹿对人的存活没有影响,故可忽略其存在。C[t][2]/C[t+1][2]:从t只虎和1个人中选2个出来,2个都是老虎的概率。老虎一直递减、连乘是把老虎都解决完。
代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const int INF = 2e9; 15 const LL LNF = 9e18; 16 const int MOD = 1e9+7; 17 const int MAXN = 1e5+100; 18 19 double dp[MAXN]; 20 int main() 21 { 22 int T, t, d, kase = 0; 23 scanf("%d", &T); 24 while(T--) 25 { 26 scanf("%d%d", &t,&d); 27 if(t%2) 28 printf("Case %d: 0\n", ++kase); 29 else 30 { 31 double ans = 1.0; 32 while(t) ans *= 1.0*(t-1)/(t+1), t -= 2; 33 printf("Case %d: %.10lf\n", ++kase, ans); 34 } 35 } 36 }