Birthday Paradox LightOJ - 1104

本文探讨了生日悖论问题,即在一个包含特定数量的人群中,至少两人拥有相同生日的概率超过50%的情况。通过数学计算得出,在不同长度的年份中(如地球的365天或火星的669天),所需人数的最小值。
部署运行你感兴趣的模型镜像

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 ≤ 105105) 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

Solution

已知某星球一年有多少天,要求的是至少两个人的生日在一年之中的同一天,最少需要找多少人。

逆向思维

地球上的n个人的生日不在同一天的概率:P=365*365/364*365*…*(1+365-n)/365,有了这个反向找最小的n即可

#include<bits/stdc++.h>

#define ll long long
#define L(u) u<<1
#define R(u) u<<1|1
using namespace std;
const int MX = 101;
int T, n, ans;
int val[MX];
double f[MX*MX];
double p[MX],P;

int main() {
    //freopen("../in", "r", stdin);
    scanf("%d",&T);
    for (int I=1;I<=T;++I) {
        printf("Case %d: ",I);
        scanf("%d",&n);
        P=1;
        ans=1;
        while (P>0.5) {
            ans++;
            P*=1.0*(n+1-ans)/n;
        }
        printf("%d\n",ans-1);
    }

    return 0;
}

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值