H - Birthday Paradox (生日謬論 思維)

H - Birthday Paradox

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




生日悖論

 編輯
生日悖論,指如果一個房間裏有23個或23個以上的人,那麼至少有兩個人的生日相同的概率要大於50%。這就意味着在一個典型的標準小學班級(30人)中,存在兩人生日相同的可能性更高。對於60或者更多的人,這種概率要大於99%。從引起邏輯矛盾的角度來說生日悖論並不是一種悖論,從這個數學事實與一般直覺相牴觸的意義上,它才稱得上是一個悖論。大多數人會認爲,23人中有2人生日相同的概率應該遠遠小於50%。計算與此相關的概率被稱爲生日問題,在這個問題之後的數學理論已被用於設計著名的密碼攻擊方法:生日攻擊

悖論內容

編輯
著名的生日悖論
23個人裏有兩個生日相同的人的機率有多大呢?
居然有50%
問題是這樣的: 如果一個房間裏有23個或23個以上的人,那麼至少有兩個人的生日相同的概率要大於50%。這就意味着在一個典型的標準小學班級(30人)中,存在兩人生日相同的可能性更高。對於60或者更多的人,這種概率要大於99%。
不計特殊的年月,如閏二月。
先計算房間裏所有人的生日都不相同的概率,那麼
第一個人的生日是 365選365
第二個人的生日是 365選364
第三個人的生日是 365選363
:
:
:
第n個人的生日是 365選365-(n-1)
所以所有人生日都不相同的概率是:
那麼,n個人中有至少兩個人生日相同的概率就是:
所以當n=23的時候,概率爲0.507
當n=100的時候,概率爲0.999999692751072
對於已經確定的個人,生日不同的概率會發生變化。下面用隨機變量計算:
令X[i,j]表示第i個人和第j個人生日不同的概率,則易知任意X[i,j]=364/365
令事件A表示n個人的生日都不相同
P(A)=
 
解P(A)<1/2,由對數可得:n>=23
相比之下,隨機變量也同樣的簡單易懂
,且計算起來要方便得多


code:
#include<iostream>
#include<cstdio>
using namespace std;  
int main() {  
    int t;  
    cin >> t;
    int k=1;  
    while(t--) {  
        int n,ans=1;  
        double p=1;
        cin >> n;     
        for(int j=n-1; j>0; j--) {  
            p=p*(j*1.0)/(n*1.0);  
            if(p<=0.5) {  
                break;  
            }  
            ans++;   
        }  
        printf("Case %d: %d\n",k++,ans);   
    }  
    return 0;  
}  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章