Birthday Paradox lightoj 1104 生日悖论(概率)

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 is669 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

题意:求至少有两个人生日在同一天的概率,问你至少邀请多少个人

分析:看见至少两个字,我们当然想起来需要求两个人都不在同一天生日的概率了。若第一个人生日任选一天,概率为1,第二个人生日选择的概率只能为364/365,第三个人生日选择的概率为363/365......以此类推。。那么概率为1*364/365*363/365...

注意:问你邀请多少个人,所以最终答案要减1.

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

#define INF 0x3f3f3f3f
const int maxn = 4007;

typedef long long LL;

int main()
{
    int T, n, k, cnt=1;
    double ans;

    scanf("%d", &T);

    while(T --)
    {
        scanf("%d", &n);
        k = 0;
        ans = 1;

        while(1-ans<0.5)
        {
            ans *= (n-k)*1.0/n;
            k ++;
        }

        printf("Case %d: %d\n", cnt++, k-1);
    }
    return 0;
}

时间: 2024-10-14 11:21:17

Birthday Paradox lightoj 1104 生日悖论(概率)的相关文章

LightOj 1104 - Birthday Paradox(生日悖论概率)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1104 题意:一年365天,在有23个人的情况下,这23个人中有两个人生日相同的概率是大于 0.5 的: 现在在不同的星球上一年有n天,求出x,至少有 x 个人才能使得这 x 人中有两个人的生日相同的概率是>=0.5的:现在除了自己之外还要 x 个人,求x: 我们按 n = 365 算的话,那么有x个人,这些人生日都不相同的概率是 p = 1 * 364/365 * 363/365 *

Birthday Paradox LightOJ - 1104

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 b

密码学经典之生日悖论与生日攻击【详解】

生日悖论 在算法导论书上看到个比较有意思的概率算法,在这里加上自己的理解分享下: 上次刚看同学发的朋友圈说道:“两个人同一间宿舍,而且同年同月同日生,这个缘分真的是醉了”,当时我也是醉醉的,看了这个算法后才发现,屋里有23个人,那么就可以50%的概率生日是一样的. 是这样子证明的: 首先,假设屋子里有K个人,分别对他们编号1,2,3….k号.不考虑闰年的情况,那么一年就有n=365天,首先还是要假设生日是均匀分布在一年的n天中(喜欢在春天生就都在春天生这就不均匀了),然后还要假设两个人生日相互独

生日悖论

假设你参加一个舞会,舞池里有30个人,请问是其中某两个人有相同的生日可能性更大呢?还是没有哪两个人有相同生日的可能性更大? 这就是所谓的生日悖论,直觉上我们会觉得一年有365天,如果只有30个人,那么存在两个人同一天生日的可能性应该很低.但是与直觉上的认识相违背的是,通过严密的数学推导可以证明这个可能性其实相当高.特别地,对于60或者更多的人,这种概率甚至要大于99%. 假设每个人的生日是一年365天中随机的一天,每个人都是独立且均匀地随机选取的,在这个假定下,可以建立分析该问题的数学模型.注意

有趣的概率算法--生日悖论

在算法导论书上看到个比较有意思的概率算法,在这里加上自己的理解分享下: 上次刚看同学发的朋友圈说道:“两个人同一间宿舍,而且同年同月同日生,这个缘分真的是醉了”,当时我也是醉醉的,看了这个算法后才发现,屋里有23个人,那么就可以50%的概率生日是一样的. 是这样子证明的: 首先,假设屋子里有K个人,分别对他们编号1,2,3….k号.不考虑闰年的情况,那么一年就有n=365天,首先还是要假设生日是均匀分布在一年的n天中(喜欢在春天生就都在春天生这就不均匀了),然后还要假设两个人生日相互独立(什么双

LightOJ 1104

题意: 给你一年有n天,求至少有m人使得至少有两个人在同一天生日的概率不少于0.5. 分析: 任意两个人不在同一天生日的概率为C(n,m)*m!/n^m,它的对立事件A为至少有两个人在同一天生日,则P(A) = 1 - C(n,m)*m!/n^m = 1 - P(n,m)/n^m(后一个P表示排列); 根据题意有P(A) >= 0.5 即 P(n, m)/n^m <= 0.5. 该式的展开式为 p = n/n*(n-1)/n*(n-2)/n*...*(n-m+1)/n, 因此只要判断该式的累乘

LightOJ 1104 - Birthday Paradox【概率】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1104 题意:生日驳论,求最小满足条件的人数 代码: #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <bitset> #include <math.h> #include <cty

LightOJ 1104 Birthday Paradox

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

Light OJ 1104 Birthday Pardo(生日悖论)

ime 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 are 23 people in