Description

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天,你要找你的朋友来參加party,且要求至少有两个人生日同样的概率大于等于0.5的最少人数,这个就是你要邀请的最小人数。

思路:

求至少有两个人生日同样的情况实在有非常多。所以我们逆向思维。求出随意两个人生日都不同样的概率。当达到要求是就退出循环,算的的结果就是最小人数,要注意的是

我们这里要减去自己,应为是要邀请的人数,还要注意防止数据溢出,因此须要边乘边除。參照算法竞赛与入门经典P324-325就能解决这道题目。

代码:

#include<cstdio>
double birthday(int n)
{
double ans=1.0;
int m=0;
for(int i=0;; i++)
{
ans*=(double)(n-i)/n;
m++;
if(1.0-ans>=0.5)
break; }
return m; }
int main()
{
int T,n,m;
int ans;
scanf("%d",&T);
int test=1;
while(T--)
{
scanf("%d",&n);
ans=birthday(n)-1;
printf("Case %d: %d\n",test++,ans);
} return 0;
}

F - 概率(经典问题)的更多相关文章

  1. 数学概念——F 概率(经典问题)birthday paradox

    F - 概率(经典问题) Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit S ...

  2. 【微信开发】PHP中奖概率经典算法实例

    $arr=array("50","30","20"); //这里简单列出三个数 $pro = array_sum($arr); // 概率数 ...

  3. Light OJ 1104 第六周F题

    F - 概率(经典问题) Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu   Descri ...

  4. GOOD BYE OI

    大米饼正式退役了,OI给我带来很多东西 我会的数学知识基本都在下面了 博客园的评论区问题如果我看到了应该是会尽力回答的... 这也是我作为一个OIer最后一次讲课的讲稿 20190731 多项式乘法 ...

  5. Python入门笔记(13):列表解析

    一.列表解析 列表解析来自函数式编程语言(haskell),语法如下: [expr for iter_var in iterable] [expr for iter_var in iterable i ...

  6. R语言︱画图

    笔者寄语:不论画啥,你先plot准没错. plot 二维坐标绘图 dotchart 点图 barplot 条形图 hist 直方图 pie 饼图 points 添加点 lines 添加线 text 添 ...

  7. matlab 常用函数

    Matlab常用函数 Matlab的内部常数  eps   浮点相对精度  pi  圆周率  exp  自然对数的底数e  i 或j  虚数单位  Inf或 inf  无穷大 Matlab概率密度函数 ...

  8. day25 面向对象继承,多态,

    这两天所学的都是面向对象,后面还有几天也是它,面向对象主要有三个大的模块,封装,继承,多态,(组合),昨天主要讲了面向对象的命名空间,还有组合的用法,今天是讲的继承还有继承里面所包括的钻石继承,以及多 ...

  9. day 7 -10 集合,文本、文件操作,函数

    day7 一.回顾 1.列表和字典在循环里边尽量不要删除元素,很麻烦 2.元组:如果元组里边只有一个逗号,且不加逗号,次元素是什么类型,就是什么类型. 二.集合 ''' 集合:可变的数据类型,它里边的 ...

随机推荐

  1. OOP & DOM

    OOP & DOM let Dom = Dom || {}; Dom = { checkValType(val) { let typeString = Object.prototype.toS ...

  2. iOS-字体UIFont的lineHeight与pointSize

    首先我们来看一看UIFont的API里面有哪些属性: // Font attributes @property(nonatomic,readonly,strong) NSString *familyN ...

  3. hdoj 1175 连连看

    连连看 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  4. 程序员编写API遵循法则

    Api理解: 1.遵循函数.数据库字段命名规则 2.思考项目的远景(扩展性) 3.思考团队调用是否方便及舒服 4.思考是否有灵动性

  5. 17.2.10 NOIP模拟赛 聪哥的工资

    聪哥的工资 (money/money.in/money.out) 时限1000ms 内存256MB 题目描述 lwher: 了体验劳苦大众的生活,聪哥在暑假参加了打零工的活动,这个活动分为n个工作日, ...

  6. *UOJ#223. 【NOI2016】国王饮水记

    $n \leq 8000$的数列,问不超过$m \leq 1e9$次操作后第一个数字最大是多少.操作:选一些数,把他们变成他们的平均值.需要保留$p \leq 3000$位小数,提供了一个小数高精度库 ...

  7. pyspider 爬虫教程(一):HTML 和 CSS 选择器

      虽然以前写过 如何抓取WEB页面 和 如何从 WEB 页面中提取信息.但是感觉还是需要一篇 step by step 的教程,不然没有一个总体的认识.不过,没想到这个教程居然会变成一篇译文,在这个 ...

  8. Python 文本(txt) 转换成 EXCEL(xls)

    #!/bin/env python # -*- encoding: utf-8 -*- #------------------------------------------------------- ...

  9. Scala不使用null 而使用Option,None,Some的好处

    刚接触Scala时就很奇怪, 为什么Java已经有null了,却偏偏还要弄出个None 后来依然我行我素在Scala里使用null, 结果就是经常被NullPointerException折磨得阴魂不 ...

  10. python的特殊方法总结

    1.跟运算相关的特殊方法 2.跟运算符相关的方法