LightOJ 1104 生日悖论(Birthday paradox)(概率抽屉原理)

生日悖论(Birthday paradox)

Description

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

 假设有n个人在同一房间内,如果要计算有两个人在同一日出生的机率,在不考虑特殊因素的前提下,例如闰年、双胞胎,假设一年365日出生概率是平均分布的(现实生活中,出生机率不是平均分布的)。

  计算机率的方法是,首先找出p(n)表示n个人中,每个人的生日日期都不同的概率。假如n > 365,根据鸽巢原理其概率为0,假设n ≤ 365,则概率为:

  \bar p(n) = 1 \cdot \left(1-\frac{1}{365}\right) \cdot \left(1-\frac{2}{365}\right) \cdots \left(1-\frac{n-1}{365}\right) =\frac{364}{365} \cdot \frac{363}{365} \cdot \frac{362}{365} \cdots \frac{365-n+1}{365}

  因为第二个人不能跟第一个人有相同的生日(概率是364/365),第三个人不能跟前两个人生日相同(概率为363/365),依此类推。用阶乘可以写成如下形式:{ 365! \over 365^n (365-n)! }

  p(n)表示n个人中至少2人生日相同的概率:

  p(n) = 1 - \bar p(n)=1 - { 365! \over 365^n (365-n)! }

  n≤365,根据鸽巢原理, n大于365时概率为1。

  当n=23发生的概率大约是0.507。其他数字的概率用上面的算法可以近似的得出来:

n p(n)
10 12%
20 41%
30 70%
50 97%
100 99.99996%
200 99.9999999999999999999999999998%
300 1 − (7 × 10−73)
350 1 − (3 × 10−131)
≥366 100%

  注意所有人都是随机选出的:作为对比,q(n)表示房间中 n个其他人中与特定人(比如你)有相同生日的概率:

   q(n) = 1- \left( \frac{364}{365} \right)^n


#include<stdio.h>  
int main()  
{  
    int T,n,i;   
    scanf("%d",&T);  
    int k=0;  
    while(T--)  
    {  
        scanf("%d",&n);  
        double p=1.0;  
        int num=0;  
       for(i=0;; i++)  
       {  
          p=p*(1-(double)i/n);  
           num++;  
          if(1.0-p>=0.5)  
            break;  
       }  
        printf("Case %d: %d\n",++k,num-1);  
    }  
    return 0;  
}  


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值