Description
一个抽奖盒里有nn个写着不同人的名字的卡片,你可以往其中放任意张写着自己名字的卡片,然后会从抽奖盒里不放回的取出张卡片,如果这pp张卡片中恰有一张写有你的名字的卡片视为中奖,问中奖的最大概率
Input
两个整数
Output
输出中奖的最大概率
Sample Input
3 2
Sample Output
0.6
Solution
假设放了aa张卡片,那么中奖概率
考虑p(a)p(a)和p(a+1)p(a+1)的大小关系,p(a+1)−p(a)=p(p−1)n−p+1∏i=1an−p+in+i(a−n−p+1p−1)p(a+1)−p(a)=p(p−1)n−p+1∏i=1an−p+in+i(a−n−p+1p−1)
故当a≤⌊n−p+1p−1⌋a≤⌊n−p+1p−1⌋时有p(a+1)≥p(a)p(a+1)≥p(a),否则p(a)≥p(a+1)p(a)≥p(a+1),故当a=⌊n−p+1p−1⌋+1=⌊np−1⌋a=⌊n−p+1p−1⌋+1=⌊np−1⌋时p(a)p(a)最大
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100001;
#define eps 1e-8
double Solve(int n,int p,int a)
{
double ans=1;
for(int i=1;i<=a;i++)ans=ans*(n+i-p)/(n+i);
ans=ans*a*p/(n-p+1);
return ans;
}
int main()
{
int n,p;
while(~scanf("%d%d",&n,&p))
{
int a=n/(p-1);
double ans=Solve(n,p,a);
printf("%.10f\n",ans);
}
return 0;
}