F - 概率(经典问题)
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 - 概率(经典问题)的更多相关文章
- 数学概念——F 概率(经典问题)birthday paradox
F - 概率(经典问题) Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit S ...
- 【微信开发】PHP中奖概率经典算法实例
$arr=array("50","30","20"); //这里简单列出三个数 $pro = array_sum($arr); // 概率数 ...
- Light OJ 1104 第六周F题
F - 概率(经典问题) Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Descri ...
- GOOD BYE OI
大米饼正式退役了,OI给我带来很多东西 我会的数学知识基本都在下面了 博客园的评论区问题如果我看到了应该是会尽力回答的... 这也是我作为一个OIer最后一次讲课的讲稿 20190731 多项式乘法 ...
- Python入门笔记(13):列表解析
一.列表解析 列表解析来自函数式编程语言(haskell),语法如下: [expr for iter_var in iterable] [expr for iter_var in iterable i ...
- R语言︱画图
笔者寄语:不论画啥,你先plot准没错. plot 二维坐标绘图 dotchart 点图 barplot 条形图 hist 直方图 pie 饼图 points 添加点 lines 添加线 text 添 ...
- matlab 常用函数
Matlab常用函数 Matlab的内部常数 eps 浮点相对精度 pi 圆周率 exp 自然对数的底数e i 或j 虚数单位 Inf或 inf 无穷大 Matlab概率密度函数 ...
- day25 面向对象继承,多态,
这两天所学的都是面向对象,后面还有几天也是它,面向对象主要有三个大的模块,封装,继承,多态,(组合),昨天主要讲了面向对象的命名空间,还有组合的用法,今天是讲的继承还有继承里面所包括的钻石继承,以及多 ...
- day 7 -10 集合,文本、文件操作,函数
day7 一.回顾 1.列表和字典在循环里边尽量不要删除元素,很麻烦 2.元组:如果元组里边只有一个逗号,且不加逗号,次元素是什么类型,就是什么类型. 二.集合 ''' 集合:可变的数据类型,它里边的 ...
随机推荐
- java-dispose方法
今天在编一道JAVA例题时,看到 dispose 这个方法,在程序中是用来关闭一个GUI页面的.这也让我想到了setVisible(false)方法.在网上查了意思,dispose()即释放所有本机屏 ...
- LAMP总四部分
第一部分 1. 安装mysqlcd /usr/local/src/ 免安装编译二进制的包wget http://syslab.comsenz.com/downloads/linux/mysql-5.1 ...
- BZOJ 1864:[Zjoi2006]三色二叉树(树DP)
三色二叉树 问题描述 输入 仅有一行,不超过500000个字符,表示一个二叉树序列. 输出 输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色. 样例输入 1122002010 ...
- CentOS下Nginx部署React静态应用
查看CentOS版本: cat /etc/redhat-release 安装nginx: yum install nginx 查看nginx版本: nginx -v 启动nginx: systemct ...
- 在vue中使用sass
首先安装node-sass和sass-loader cnpm install node-sass && sass-loader --save 在webpack.config.js 的m ...
- Java EE 学习(5):IDEA + maven + spring 搭建 web(1)
参考:http://www.cnblogs.com/lonelyxmas/p/5397422.html http://www.ctolib.com/docs-IntelliJ-IDEA-c--1590 ...
- IOS YYKit 源码解析
https://blog.csdn.net/weixin_33874713/article/details/87034047
- python优雅写法
在这篇文章中我将和大家分享一些真正有用的技巧和窍门,这些技巧和窍门你们之前可能并不知道.所以不浪费时间了,让我们直接来看看这些内容吧: 枚举 之前我们这样操作: 1 2 3 4 i = 0 for i ...
- ubuntu下使用OBS开斗鱼直播
系统环境:ubuntu 15.10,OBS Studio 0.13.1 OBS是可以在linux,windows,mac下直播的开源软件,官方地址:https://obsproject.com/ 斗鱼 ...
- Scrapy笔记:持久化,Feed exports的使用
首先要明确的是,其实所有的FeedExporter都是类,里面封装了一般进行io操作的方法.因此,要怎么输出呢?其实从技术实现来说,在生成item的每一步调用其进行储存都是可以的,只不过为了更加符合s ...