LightOJ

来源:互联网 发布:电气制图软件下载 编辑:程序博客网 时间:2024/06/14 16:28

Sometimes some mathematical results are hard to believe. One of the common problems is the birthday paradox. Suppose you are in a party where there are 23 people including you. What is the probability that at least two people in the party have same birthday? Surprisingly the result is more than 0.5. Now here you have to do the opposite. You have given the number of days in a year. Remember that you can be in a different planet, for example, in Mars, a year is 669 days long. You have to find the minimum number of people you have to invite in a party such that the probability of at least two people in the party have same birthday is at least 0.5.

Input
Input starts with an integer T (≤ 20000), denoting the number of test cases.

Each case contains an integer n (1 ≤ n ≤ 105) in a single line, denoting the number of days in a year in the planet.

Output
For each case, print the case number and the desired result.

Sample Input
2
365
669
Sample Output
Case 1: 22
Case 2: 30

这个题我想过二分答案然后用容斥倒着算进去…
但是看着T的大小….
很挫

百度题解发现竟然只要算出一个一个的占坑只要不重合概率小于二分之一就可以了。。
我真是傻了

不过这个思路记下来了,只要概率题不好做不妨反着想

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;int main(){    int T;    cin >> T;    int u = 0;    while (T--)    {        int n;        cin >> n;        double p = 1;        int r;        for (int a = 1; a <= n; a++)        {            p *= (n - a) / double(n);            if (p <= 0.5)            {                r = a;                break;            }        }        printf("Case %d: %d\n", ++u, r);    }}
原创粉丝点击