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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-25 03:10:34

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

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

$arr=array("50","30","20"); //这里简单列出三个数 $pro = array_sum($arr); // 概率数组的总精度 foreach ($arr as $key=>$value){ $randnum = mt_rand(1,$pro); if($randnum<=$value){ $res = $value; //这里是中奖的时候,一般$res会赋值和其概率相对应的奖品 break; }else {

Light OJ 1104 第六周F题

F - 概率(经典问题) Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu 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 ar

二十五.面向对象(经典类和新式类)

经典类和新式类 class A(object): pass class B(A): pass class C(B): pass class D(object): pass class E(D,C): pass class F(object): pass class G(F): pass class H(C,G): pass class Foo(E,H): pass # a1=Foo() print(Foo.__mro__) # (<class '__main__.Foo'>, <clas

fasttext源码剖析

fasttext源码剖析 目的:记录结合多方资料以及个人理解的剖析代码: https://heleifz.github.io/14732610572844.html http://www.cnblogs.com/peghoty/p/3857839.html 一:代码总体模块关联图: 核心模块是fasttext.cc以及model.cc模块,但是辅助模块也很重要,是代码的螺丝钉,以及实现了数据采取什么样子数据结构进行组织,这里的东西值得学习借鉴,而且你会发现存储训练数据的结构比较常用的手段,后期可

《转》python学习(12)-列表解析

一.列表解析 列表解析来自函数式编程语言(haskell),语法如下: [expr for iter_var in iterable] [expr for iter_var in iterable if cond_expr] 第一种语法:首先迭代iterable里所有内容,每一次迭代,都把iterable里相应内容放到iter_var中,再在表达式中应用该iter_var的内容,最后用表达式的计算值生成一个列表. 第二种语法:加入了判断语句,只有满足条件的内容才把iterable里相应内容放到i

day06_雷神_面向对象初识

day_06 递归函数 自己用自己.一般递归100多次,都没有解决的问题,放弃递归. count = 0 def func1(): global count count += 1 print(count) func1() func1() 默认递归深度:998 设置递归深度 import sys sys.setrecursionlimit(20000) count = 0 def func1(): global count count += 1 print(count) func1() func1

斐波那契数列的记忆搜索

目录 摘要 斐波那契数列 经典递归分析 动态规划分析 摘要 本blog通过"斐波那契数列求值"这个经典问题,分析并说明"从单一递归到记忆搜索"这个思想过程.本blog是整个动态规划学习的一部分.(记忆搜索是动态规划的递归写法) 斐波那契数列 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为"兔子数列",指的是这样一个数列:

过分过分进货价获国家

http://f.dangdang.com/group/24554/3491082/http://f.dangdang.com/group/24554/3491087/http://f.dangdang.com/group/24554/3491094/http://f.dangdang.com/group/24554/3491099/http://f.dangdang.com/group/24554/3491105/http://f.dangdang.com/group/24554/349111

我们找个地方看好戏

http://v.qq.com/page/f/y/4/m041433ssun.html http://v.qq.com/page/f/y/4/m041433ssun.html http://v.qq.com/page/f/y/4/m04143o3lhg.html http://v.qq.com/page/f/y/4/m04144675h3.html http://v.qq.com/page/f/y/4/m04144k1k1j.html http://v.qq.com/page/f/y/4/m04