LightOJ 1104 Birthday Paradox

概率,暴力。

首先假设答案为$ans$,天数为$n$天,那么不出现至少二人生日相同的概率为$(n-0)/n*(n-1)/n*(n-2)/n*......*(n-(ans-1))/n$,要求这个概率小于等于$0.5$。

写了个二分发现答案不会超过$400$,所以前缀乘打个长度为$400$的表,二分一下答案或者暴力跑$400$就可以了。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-6;
void File()
{
    freopen("D:\in.txt","r",stdin);
    freopen("D:\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar();
    x = 0;
    while(!isdigit(c)) c = getchar();
    while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
}

int T,n;
double q[100010];

bool check(int x)
{
    if(q[x-1]<=0.5) return 1;
    return 0;
}

int main()
{
    scanf("%d",&T); int cas=1;
    while(T--)
    {
        scanf("%d",&n);
        q[0]=1.0;
        for(int i=1;i<=400;i++) q[i]=q[i-1]*(n-i)/n;
        int L=2, R=400, ans;
        while(L<=R)
        {
            int m=(L+R)/2;
            if(check(m)) ans=m,R=m-1;
            else L=m+1;
        }
        if(n==1) ans=2;
        printf("Case %d: %d
",cas++,ans-1);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/6289959.html