Birthday Paradox LightOJ - 1104 (概率期望)

该博客讨论了概率问题中的生日悖论,提出了在一个特定天数的年份里,需要邀请多少人参加聚会,使得至少两人生日相同的概率达到或超过50%。通过数学计算,给出了在不同天数的年份中,这个最小人数的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ≤ 10^5) 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

大致题意:告诉你现在一年的天数n,问除了主人,至少还需邀请多少人来家里聚会,才能使得此时所有人中至少有两个人的生日是同一天的概率大于0.5。

思路:所有人中至少有两个人的生日是同一天的概率就等于1减去所有人的生日都不是同一天的概率,那么问题就变的简单了,假设此时一年的天数为n,至少要邀请x人,那么所有人的生日都不是同一天的概率即为(n-1) / n * (n-2)/n * ……*(n-x)/n,暴力去求下最小的x使得该答案的值小于0.5即可

代码如下

#include<bits/stdc++.h>
const double eps=1e-7;//注意精度,这里至少1e-7取1e-6会wa
using namespace std;

int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        int n;
        scanf("%d",&n);
        double s=1;
        int ans=0;
        while(1)
        {
            if(s-0.5<eps) break;
            ans++;
            s*=1.0*(n-ans)/n;
        }
        printf("Case %d: %d\n",cas,ans);    
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值