概率DP——I - Birthday Paradox LightOJ - 1104

博客目录

一、原题

题目传送门(vjudge有密码,light的题目就不发原题网址了自行搜索吧)

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 are23 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人的聚会,包括你在内。 党内至少有两个人有相同生日的概率是多少? 令人惊讶的是,结果超过0.5。 现在你必须做相反的事情。 你给了一年的天数。 记住,你可以在一个不同的星球,例如在火星,一年是669天。 你必须找到你在一个聚会中邀请的最少人数,使得聚会中至少有两个人的同一生日的概率至少为0.5。

三、分析

一年有d天n个人时任意两个人的生日都不相同生日的概率为:n个人生日情况一共有 d的n次方

    则 概率1-p=分子/分母,分子=d*(d-1)*(d-2)*...一共乘n个表示n个人            分母=d的n次方

好了,所以很容易就得到一个连乘式:1-p=连乘积((d-i)/d),i=0,1,2.... 直到乘到1-p小于0.5就停止

四、AC代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int T,n;
	cin>>T;
	double z;
	int i;
	int cs=1;
	while(T--)
	{
		cin>>n;
		z=1;
		for(i=1;z>0.5;i++)
		{
			z=z*(n-i)/n;
		}
		printf("Case %d: ",cs++);
		cout<<i-1<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/greybtfly/article/details/80551736