hdu_4465_Candy
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的更多相关文章
随机推荐
- 2-4 Sass的函数功能-颜色函数
			RGB颜色函数-RGB()颜色函数 在 Sass 的官方文档中,列出了 Sass 的颜色函数清单,从大的方面主要分为 RGB , HSL 和 Opacity 三大函数,当然其还包括一些其他的颜色函数, ... 
- 基础架构之GitLab
			Git几乎是软件开发人员的必备工具了,关于代码管理,公司都一般都会搭建自己的仓库,关于GitLab的详细介绍参见官方网站详见 https://about.gitlab.com,这篇文章主要介绍安装及使 ... 
- Java 之变量和常量(2)
			Java中的关键字: Java 语言中有一些具有特殊用途的词被称为关键字.关键字对 Java 的编译器有着特殊的意义,在程序中应用时一定要慎重哦!! Java 中常用关键字: 问:这么多,记不住啊.. ... 
- bootstrap 默认显示1899问题
			今天使用bootstrap的 dateTimePicker控件时候,又碰到了去年的 显示 1899年的问题,之前解决过,但是忘记了.就记得 他的代码里面有一段是 说格式不正确或者 时间格式小于某个值时 ... 
- [转] RISC-V架构介绍
			1. RISC-V和其他开放架构有何不同 如果仅从"免费"或"开放"这两点来评判,RISC-V架构并不是第一个做到免费或开放的处理器架构. 在开始之前,我们先通 ... 
- SQL Server ->> 利用CONVERT/STR/FORMAT函数把浮点型数据格式化/转换成字符串
			在SQL Server下想把数字(包括浮点型和整型)转换成字符串,保留数据原本的样子或者根据需要转换成另外指定的格式可能就不仅仅是一条CAST(XXXX AS NVARCHAR)这么简单的事情了. 无 ... 
- python多进程与协程
			1.进程的概念 什么是进程->CPU在同一时刻只能处理一个任务,只是因为cpu执行速度很快. cpu在各个任务之间来回的进行切换. 进程的概念:正在进行的一个过程或者说一个任务,而负责执行任务的 ... 
- 小程序——使用Easy Mock
			使用Easy Mock 一.什么是Easy Mock Easy Mock 是一个可视化的能快速生成模拟数据的持久化服务.在实际开发中常见的Mock方式一般是将模拟数据直接写在代码里,利用Java ... 
- ieHTTPHeaders使用方法
			在http://www.blunck.se/iehttpheaders.html下载软件打开IE浏览器查看-->浏览器栏-->ieHTTPHeaders可以查看httpheader tra ... 
- python .loc vs .iloc区别
			1.loc意义:通过行标签索引行数据 例: loc[n]表示索引的是第n行(index 是整数) loc[‘d’]表示索引的是第’d’行(index 是字符) 2. .iloc :通过行号获取行数 ... 
