首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【LightOJ】1104 - Birthday Paradox(组合数学)

【LightOJ】1104 - Birthday Paradox(组合数学)

作者头像
FishWang
发布2025-08-27 10:30:47
发布2025-08-27 10:30:47
6300
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

1104 - Birthday Paradox

PDF (English)

Statistics

Forum

Time Limit: 2 second(s)

Memory Limit: 32 MB

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 least0.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

Output for Sample Input

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,最后结果减一就行了。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Input
  • Output
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档