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. icheck 动态设置选中,判断是否选择

    $(this).iCheck('check'); //启用禁用上级编号             $('#OnPar').on('ifUnchecked', function (event) {     ...

  2. PYTHON资源入口汇总

    Python资源入口汇总 官网 官方文档 教程和书籍 框架 数据库 模板 工具及第三方包 视频 书籍 博客 经典博文集合 社区 其他 整理中,进度30% 官网 入口 官方文档 英文 document ...

  3. BZOJ2396 神奇的矩阵 【随机化 + 矩乘】

    题目链接 BZOJ2396 题解 一种快速判断两个矩阵是否相等的方法: 对于两个\(n * n\)矩阵,两边同时乘一个\(n * 1\)的随机矩阵,如果结果相等,那么有很大概率两个矩阵相等 如果左边是 ...

  4. 【CCF】JSON查询

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  5. POJ1692 Crossed Matchings

    Time Limit: 1000MS     Memory Limit: 10000K Total Submissions: 2738   Accepted: 1777 Description The ...

  6. repeater做成gridview【更新删除编辑等】

    原文发布时间为:2009-06-14 -- 来源于本人的百度文章 [由搬家工具导入] 不多说,不会说。。看我做的范例。。。 http://download.csdn.net/source/138556 ...

  7. 《手把手教你学C语言》学习笔记(8)--- 运算符和表达式

    C语言编程的核心是指针和库,而库的核心就是函数,函数的基本组成部分就是语句. C语言合法表达式加上分号(语句结束符)构成C函数的基本部分语句.如果只有分号没有表达式就构成空语句,空语句常常用来形成占位 ...

  8. /* Dr黄的技术博客开通啦 */

    以前懒得写技术日志, 现在终于发现重要性.. mark一下这个里程碑.

  9. springBoot 数组增加工具类包

    1.pom中加入依赖 <!--数组工具类 start--> <dependency> <groupId>org.apache.commons</groupId ...

  10. jdbc in postgres

    try { Class.forName("org.postgresql.Driver").newInstance(); String url = "jdbc:postgr ...