LightOj_1104 Birthday Paradox

题目链接

题意:

  若一年有n天, 问至少需要多少个人才能满足其中两个人生日相同的概率大于等于0.5?

思路:

  经典问题:生日悖论

  换成其互斥事件:m个人, 每个人生日都不相同的概率 ≤ 0.5 时最小人数。

  这就是邮票收集问题的变形:每个邮票至少出现一次的概率 小于等于 0.5

  等价于:

      找到最小的n, 使得:H[n] = (n / n + (n - 1) / n + (n - 2) / n + ... + 1 / n) <= 0.5

代码:

  

 1 #include <cmath>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <ctime>
 6 #include <set>
 7 #include <map>
 8 #include <list>
 9 #include <queue>
10 #include <string>
11 #include <vector>
12 #include <fstream>
13 #include <iterator>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 #define LL long long
18 #define INF 0x3f3f3f3f
19 #define MOD 1000000007
20 #define eps 1e-6
21 #define MAXN 100010
22 #define MAXM 380
23 double f[MAXN];
24 int n;
25 void init()
26 {
27     double x, y;
28     double m;
29     f[0] = 0;
30     f[1] = 1;
31     for(int i = 2; i < MAXN; i ++)
32     {
33         m = i;
34         y = i - 1.0;
35         x = 1.0;
36         for(int j = 1; j < MAXM; j ++)
37         {
38             x = x * y;
39             x /= m;
40             y --;
41             if(x <= 0.5)
42             {
43                 f[i] = j;
44                 break;
45             }
46         }
47     }
48 }
49
50 int main()
51 {
52     int T;
53     int kcase = 0;
54     init();
55     scanf("%d", &T);
56     while(T --)
57     {
58         scanf("%d", &n);
59         printf("Case %d: %.0lf\n", ++kcase, f[n]);
60     }
61     return 0;
62 }

时间: 2024-10-19 18:55:41

LightOj_1104 Birthday Paradox的相关文章

LightOJ1104---Birthday Paradox (概率)

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

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

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

light oj 1104 Birthday Paradox (概率题)

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

[delphi技术] (SQL DBE、ADO连接)+(Firebird火鸟+DbExpress)+(VF DBF数据库)+(DB Paradox)

DBE 连接SQL Server显然用ADO或DBEXPRESS更有优势,起码连接起来比较方便. BDE的话可以用如下方法:(以下以Delphi7为例,其它版本的DELPHI请自己摸索一下,不过基本相差不大) 1.启动Delphi,选择菜单项 Database->Explorer,在左侧Database列表里面右键,选择New,在弹出的驱动对话框里面选择MSSQL,确定,然后在左侧可以更改名字,如:TEST,然后在选中TEST在右侧选项里面添入ServerName(服务器名) UserName(

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

codeforces 711E. ZS and The Birthday Paradox 概率

已知一年365天找23个人有2个人在同一天生日的概率 > 50% 给出n,k ,表示现在一年有2^n天,找k个人,有2个人在同一天生日的概率,求出来的概率是a/b形式,化到最简形式,由于a,b可能非常大,对a,b分别%(10^6+3) 注意,这道题是先化到最简,再分别取模 首先,特判 k > 2^n 时,a = 1,b = 1 没有2个人同一天生日的概率为: 2^n * (2^n - 1) * ... * (2^n - k + 1) / 2^(nk) 所以a,b化简之前分别是: a = 2nk

Codeforces 711E ZS and The Birthday Paradox(乘法逆元)

[题目链接] http://codeforces.com/problemset/problem/711/E [题目大意] 假设一年有2^n天,问k个小朋友中有两个小朋友生日相同的概率. 假设该概率约分后为 p / q ,输出p , q对1000003取模的解. [题解] 当k比天数要大时是肯定成立的,否则答案为1-A(2n,k) / (2n)k, 考虑A(2n,k)=2n*(2n-1)*……*(2n-k+1),所以分子和分母的最大公约数是2的幂次,暴力计算分子分母,以及计算最大公约数的逆元,就可

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 *