点击打开题目
1104 - Birthday Paradox PDF (English)StatisticsForum Time Limit: 2 second(s)Memory Limit: 32 MBSometimes 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 least0.5.
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.
For each case, print the case number and the desired result.
2
365
669
Case 1: 22
Case 2: 30
首先要搞明白至少两个人同一天生日的概率怎么求,设人数为m,总天数为n。
我们用容斥原理,先求所有人生日都不同的概率,用1减去它即可。
m个人每人选一个当做生日:A(m,n)
m个人总共可能有的生日的数:n^m
那么题目就是求:1 - A(m,n)/ n^m >= 0.5
求这个最小m,最后结果减一就行了。
代码如下:
#include <stdio.h> #include <cstring> #include <algorithm> using namespace std; #define CLR(a,b) memset(a,b,sizeof(a)) #define INF 0x3f3f3f3f #define LL long long int main() { int n; int ans; double t; int u; int Case = 1; scanf ("%d",&u); while (u--) { scanf ("%d",&n); ans = 2; t = 1; for ( ; ans <= n ; ans++) { t *= (n-ans+1); t /= n; if (t <= 0.5) break; } printf ("Case %d: ",Case++); printf ("%d\n",ans-1); } return 0; }