题目链接:https://vjudge.net/problem/LightOJ-1104

1104 - Birthday Paradox
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 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

Output for Sample Input

2

365

669

Case 1: 22

Case 2: 30

题意:

问至少要多少人,才能使得至少两个人同一天生日的概率不小于0.5?其中一年的天数由题目给定。

题解:

1.至少2个人同一天生日的对立事件为:所有人的生日都不同。先求对立事件,然后再:p(C) = 1-p(!C) 。

2.假设答案为m,如果所有人的生日都不同,那么: p(!C) = A[n][m]/n^m = n/n * (n-1)/n * …… *(n-m+1)/n。

3.m从1开始枚举,当 1- p(!C) >= 0.5 时, m-1就是答案(题目求除己之外)。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e2+; int main()
{
int T, n, kase = ;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
double p = 1.0;
int m;
for(m = ; ;m++)
{
p *= 1.0*(n-m+)/n;
if(-p>=0.5) break;
}
printf("Case %d: %d\n",++kase, m-);
}
}

最新文章

  1. [内核笔记1]内核文件结构与缓存——inode和对应描述
  2. word20161220
  3. Unknown column &#39;&#39; in &#39;field list&#39;解决方案
  4. [maven] settings 文件节点配置详解
  5. 剑指offer系列50--不用加减乘除做加法
  6. css列表
  7. CMOS (1)&ndash;PMOS与NMOS
  8. 服务端API的OAuth认证实现
  9. 【原创】有关Buffer使用,让你的日志类库解决IO高并发写
  10. http目录显示时间与服务器相差8小时
  11. debug和release版本的区别
  12. Missile Command 导弹指令
  13. system调用导致子进程socket句柄泄漏问题分析
  14. jupyter notebook 小笔记
  15. video 播放本地视屏
  16. JavaScript Data.parse()转化时间戳安卓和ISO不兼容
  17. (转)公有云vr客户端tcp连接数太多造成 系统卡顿问题 [bittorrent tracker优化] -公有云常见网络问题及思路
  18. go基本操作
  19. e662. 取的图像的色彩模型
  20. 在进行分布式框架搭建的过程中,出现问题advised by org.springframework.transaction.interceptor.TransactionInterceptor.invoke(org.aopalliance.intercept.MethodInvocation)?

热门文章

  1. [原创][SW]TortoiseSVN创建本地版本控制
  2. PropertyPlaceholderConfigurer 基本用法
  3. UITableView 滚动时使用reloaddata出现 crash&#39;-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (0)&#39; Crash
  4. Android -- AsyncTask 使用和缺陷
  5. 利用VideoView播放视频
  6. 百科知识 scm文件如何打开
  7. Unity开发规范(个人习惯,仅供參考)
  8. 4.php整合Memcached
  9. 【每日Scrum】第四天(4.14) TD学生助手Sprint1站立会议
  10. python--面向对象—接口