生日悖论(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.Case 2: 30
假设有n个人在同一房间内,如果要计算有两个人在同一日出生的机率,在不考虑特殊因素的前提下,例如闰年、双胞胎,假设一年365日出生概率是平均分布的(现实生活中,出生机率不是平均分布的)。
计算机率的方法是,首先找出p(n)表示n个人中,每个人的生日日期都不同的概率。假如n > 365,根据鸽巢原理其概率为0,假设n ≤ 365,则概率为:
因为第二个人不能跟第一个人有相同的生日(概率是364/365),第三个人不能跟前两个人生日相同(概率为363/365),依此类推。用阶乘可以写成如下形式:
p(n)表示n个人中至少2人生日相同的概率:
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个其他人中与特定人(比如你)有相同生日的概率:
#include
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;
}