数学概念——F 概率(经典问题)birthday paradox

F - 概率(经典问题)
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Submit Status

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,根据鸽巢原理,n大于365时概率为1。

鸽巢原理:又称抽屉原理,或狄利克雷原理,被用来证明一些关于存在性的数学问题,并且在数论和密码学中也有广泛的应用

这里题目的意思是在N天中,两人在同一天生日的概率不超过0.5的概率,这样的人会来多少人才能满足

  理解生日悖论的关键在于领会相同生日的搭配可以是相当多的。如在前面所提到的例子,23个人可以产生种不同的搭配,而这每一种搭配都有成功相等的可能。从这样的角度看,在253种搭配中产生一对成功的配对也并不是那样的不可思议。

  换一个角度,如果你进入了一个有着22个人的房间,房间里的人中会和你有相同生日的概率便不是50:50了,而是变得非常低。原因是这时候只能产生22种不同的搭配。

程序代码:

#include <cstdio>
using namespace std;
const int L=100010;
double  d[L];
int n;
int main()
{
    int t,Case=0;
    scanf("%d",&t);
     while(t--)
    {
       scanf("%d",&n);
       int ans=0;
       double p=0,pz=1.0;
      while(p<0.5)
      {
          pz=pz*(1-ans*1.0/n);
          p=1-pz;
          ans++;
      }
       printf("Case %d: %d
",++Case,ans-1);
    }
    return 0;
}
View Code
版权声明:此代码归属博主, 请务侵权!
原文地址:https://www.cnblogs.com/www-cnxcy-com/p/4740439.html