CSU-2019 Fleecing the Raffle

Description

A tremendously exciting raffle is being held, with some tremendously exciting prizes being given out. All you have to do to have a chance of being a winner is to put a piece of paper with your name on it in the raffle box. The lucky winners of the p prizes are decided by drawing p names from the box. When a piece of paper with a name has been drawn it is not put back into the box – each person can win at most one prize. Naturally, it is against the raffle rules to put your name in the box more than once. However, it is only cheating if you are actually caught, and since not even the raffle organizers want to spend time checking all the names in the box, the only way you can get caught is if your name ends up being drawn for more than one of the prizes. This means that cheating and placing your name more than once can sometimes increase your chances of winning a prize. You know the number of names in the raffle box placed by other people, and the number of prizes that will be given out. By carefully choosing how many times to add your own name to the box, how large can you make your chances of winning a prize (i.e., the probability that your name is drawn exactly once)?

Input

There will be several test cases. Each case consists of a single line containing two integers n and p ( 2≤p≤n≤1062≤p≤n≤106 ), where n is the number of names in the raffle box excluding yours, and p is the number of prizes that will be given away.

Output

Output a single line containing the maximum possible probability of winning a prize, accurate up to an absolute error of 10−6.

Sample Input

3 2
23 5

Sample Output

0.6
0.45049857550

题解

题意:抽奖活动,可以放入任意张有自己名字的纸片参与抽奖,当且仅当带有自己名字的纸片被抽取两次时会被抓住,视作失败。共抽取p件奖品,参与抽奖的有n个人,问自己最大获奖概率是多少

设x为放入的自己名字的纸片个数,则放入x张获奖概率为

\[\frac{C_x^1Cn^{p-1}}{C_{n+x}^p}=\frac{x\times p}{n+1}\prod_{i=2}^x\frac{n-p+i}{n+i}
\]

当从x-1到x,概率乘以\(\frac{x}{x - 1}\times\frac{n-p+x}{n+x}\),递推求概率,当概率开始变小时终止循环,输出答案

#include<bits/stdc++.h>
using namespace std;
int main() {
int n, p;
while (scanf("%d%d", &n, &p) != EOF) {
double now = (double)p / (double)(n + 1.0);
double ans = 0.0;
int x = 2;
while (1) {
if (ans > now) break;
else ans = now;
now *= (double)x / (double)(x - 1.0) * (double)(n + x - p) / (double)(n + x);
x++;
}
printf("%.11lf", ans);
}
}
/**********************************************************************
Problem: 2019
User: Artoriax
Language: C++
Result: AC
Time:28 ms
Memory:2024 kb
**********************************************************************/

最新文章

  1. bzoj4398:福慧双修
  2. Bootstrap_响应式网格系统
  3. BZOJ1520 [POI2006]Szk-Schools
  4. VS2012 内容存储区指定的位置无效或者您无权访错误
  5. 用DELPHI的RTTI实现对象的XML持久化
  6. CSS设计指南之理解盒子模型
  7. javascript语句语义大全(6)
  8. git上传本地项目到github
  9. Oracle_SQL92_连接查询
  10. eclipse乱码解决
  11. Mockito单元测试实战
  12. C#控件DataGridView笔记
  13. k8s对接ceph存储
  14. minicom 安装 查看串口
  15. 针对数据泵导出 (expdp) 和导入 (impdp)工具性能降低问题的检查表 (文档 ID 1549185.1)
  16. C# 选择文件、选择文件夹、打开文件(或者文件夹) 路径中获取文件全路径、目录、扩展名、文件名称 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名!!
  17. Win7 VS2015环境编译cegui-0.8.5
  18. final 140字评论I
  19. luasql在Fedora20下的安装与使用示例
  20. git 绑定远程仓方法

热门文章

  1. LEMP (LNMP) Stack-5.4.16 (OpenLogic CentOS 7.2)
  2. app后台管理系统框架metronic的学习笔记
  3. [VC]vc中socket编程步骤
  4. IOS 某个控件出不来原因(经验分享)
  5. 阿里云服务器下安装LAMP环境(CentOS Linux 6.3)(1)
  6. GMap.Net解决方案之在WinForm和WPF中使用GMap.Net地图插件的开发
  7. C++ lambda 表达式 简介
  8. Centos7之WEB服务器
  9. phpstudy配置SSL证书的步骤(Apache环境)以及一些注意事项
  10. 利用DOM的方式点击切换图片及修改文字