题干:
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天,求邀请人数的最小值ans,满足参加生日party上至少两个人同一天生日的概率至少为0.5。
解题报告:
求至少两个人同一天不是很好求,可以求问题的对立面,考虑任意两个人都不是同一天的概率,每个人生日的概率是1/n,当邀请的人数是ans,每个人生日都不同时,概率为P。
则
所以至少两个人生日同一天的概率为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 ;
}