【LightOJ - 1104】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
題目大意:
已知星球上有n天,求邀請人數(shù)的最小值ans,滿足參加生日party上至少兩個(gè)人同一天生日的概率至少為0.5。
解題報(bào)告:
求至少兩個(gè)人同一天不是很好求,可以求問題的對立面,考慮任意兩個(gè)人都不是同一天的概率,每個(gè)人生日的概率是1/n,當(dāng)邀請的人數(shù)是ans,每個(gè)人生日都不同時(shí),概率為P。
則?
所以至少兩個(gè)人生日同一天的概率為1-P,只要1-P>0.5退出,最后答案為ans。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 10000 + 5; int n; int main() {int t,iCase=0;cin>>t;while(t--) {scanf("%d",&n);int ans = 0;double P = 1;while(P > 0.5) {ans++;P *= (1-ans*1.0/n);}printf("Case %d: %d\n",++iCase,ans);}return 0 ; }?
總結(jié)
以上是生活随笔為你收集整理的【LightOJ - 1104】Birthday Paradox(概率,思维)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: winhlp32.exe - winhl
- 下一篇: 【POJ - 2594】Treasure