LazyChild is a lazy child who likes candy very much. Despite being very young, he has two large candy boxes, each contains n candies initially. Everyday he chooses one box and open it. He chooses the first box with probability p and the second box with probability (1 - p). For the chosen box, if there are still candies in it, he eats one of them; otherwise, he will be sad and then open the other box.
He has been eating one candy a day for several days. But one day, when opening a box, he finds no candy left. Before opening the other box, he wants to know the expected number of candies left in the other box. Can you help him?

InputThere are several test cases.

For each test case, there is a single line containing an integer n (1 ≤ n ≤ 2 × 10
5) and a real number p (0 ≤ p ≤ 1, with 6 digits after the decimal).

Input is terminated by EOF.OutputFor each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is a real number indicating the desired answer.

Any answer with an absolute error less than or equal to 10
-4 would be accepted.Sample Input

10 0.400000
100 0.500000
124 0.432650
325 0.325100
532 0.487520
2276 0.720000

Sample Output

Case 1: 3.528175
Case 2: 10.326044
Case 3: 28.861945
Case 4: 167.965476
Case 5: 32.601816
Case 6: 1390.500000 求期望,用log来提高精度并且实现组合数。
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<math.h>
#define ll __int64
using namespace std;
#define N 1000005
double a[N<<1];
int main()
{
a[0]=0;
for(int i=1;i<N*2;i++)
a[i]=a[i-1]+log(1.0*i);
int n;
double p;
int t=0;
while(~scanf("%d%lf",&n,&p))
{
double ans=0,res1,res2;
for(int i=0;i<=n;i++)
{
res1=(a[2*n-i]-a[n]-a[n-i])+(n+1)*log(p)+(n-i)*log(1-p);
res2=(a[2*n-i]-a[n]-a[n-i])+log(1-p)*(n+1)+log(p)*(n-i);
ans+=exp(res1)*i+exp(res2)*i;
}
printf("Case %d: %.6f\n",++t,ans);
}
}

  

hdu_4465_Candy的更多相关文章

随机推荐

  1. JS原生隐藏显示图片,点击切换图片的效果

    今天要说的内容,看标题就都能知道了!所有知识点一览无遗啊!咱们今天的东西,是纯纯的原生JS代码, 我先说一下要求, 1.有两个按钮,内容为显示,和换, 2.当点击显示的时候,按钮文字变成隐藏,同时图片 ...

  2. Idea15 常用设置(一):JDK、SVN

      1:显示行号  File->Settings->General->Appearance 2: 代码自动补齐即使是小写字母也会弹出代码补齐提示     3:自动编译 设置 4: 设 ...

  3. EasyUI combobox 加载JSON数据

    Action返回 JSON 格式如下: jsonResult = { total=7,rows=[ {TEXT=技术支持, ID=402894ca4419acf1014419b148a10000}, ...

  4. 部署node.js的开发环境

    1.进入Node.js的官方网站下载安装包: http:nodejs.org 2.安装后打开cmd的dos窗口(在path环境变量中查看到有nodejs说明安装成功): 3.运行node.

  5. qt 样式表基本用法

    Qt样式表 QT样式表参考CSS层叠样式表设计,不同之处在于QT样式表应用于Widget世界. 可以使用QApplication::setStyleSheet()函数设置到整个应用程序上,也可以使用Q ...

  6. Android Animation 知识点速记备忘思维导图

    备注的大段文本,无法在图片中体现, 思维导图源文件放在附件中.使用 Xmind 8 制作. 附件:AndroidAnimation-xmind.zip

  7. 自学git心得-2

    趁着最近还没忙起来,抓紧更新一下学习心得. 现在的情景是,我们已经在本地创建了一个Git仓库,又想在GitHub创建一个Git仓库,并且让这两个仓库进行远程同步,这样,GitHub上的仓库既可以作为备 ...

  8. Thread调用SaveFileDialog

    public void ThreadSaveFileDialog(string sourceFileName) { Thread importThread = new Thread(new Param ...

  9. DOS 保存系统日志

    1.导出系统日志,以当前日期命名 @echo offset nowDate=%date%set tmp=%nowDate:~0,-3%set file=%tmp:/=-%系统日志.evtx echo ...

  10. DedeCms织梦后台管理员密码修改和忘记重置方法

    方法/步骤 打开dede_admin数据表: 进入你的MYSQL后台,然后在你的数据库名中,找到dede_admin这项如图,pwd下的值就是你的密码,织梦密码采取的是MD5加密,破解麻烦而且没有必要 ...