B - Candy

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Appoint description: 
System Crawler  (2014-10-17)

Description

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?
 

Input

There 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.
 

Output

For 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
 
公式

快速排列组合函数:logC(m,n),zuhe[i]其实就是i的阶乘,然后给取log值
这样zuhe[m]-zuhe[n]-zuhe[m-n]就是C(m,n)的log值
然后把其余也取对数,然后再求exp就好了!
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define M(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f using namespace std; int n;
double p;
double zuhe[]; double logC(int m,int n)
{
return zuhe[m]-zuhe[n]-zuhe[m-n];
} int main()
{
int cas = ;
zuhe[] = ;
for(int i = ;i<;i++) zuhe[i] = zuhe[i-]+log(i*1.0);
while(scanf("%d%lf",&n,&p)==)
{
double res = ;
for(int i = ;i<=n;i++)
{
res+=(n-i)*exp(logC(n+i,i)+(n+)*log(p)+(i)*log(-p));
res+=(n-i)*exp(logC(n+i,i)+(n+)*log(-p)+(i)*log(p));
}
printf("Case %d: %.16f\n",cas++,res);
}
return ;
}

2012Chengdu B (快速组合数)的更多相关文章

  1. NOIP2011多项式系数[快速幂|组合数|逆元]

    题目描述 给定一个多项式(by+ax)^k,请求出多项式展开后x^n*y^m 项的系数. 输入输出格式 输入格式: 输入文件名为factor.in. 共一行,包含5 个整数,分别为 a ,b ,k , ...

  2. 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数

    1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...

  3. UVALive 7040 Color (容斥原理+逆元+组合数+费马小定理+快速幂)

    题目:传送门. 题意:t组数据,每组给定n,m,k.有n个格子,m种颜色,要求把每个格子涂上颜色且正好适用k种颜色且相邻的格子颜色不同,求一共有多少种方案,结果对1e9+7取余. 题解: 首先可以将m ...

  4. Paths on a Grid POJ - 1942 组合数学 (组合数的快速计算)

    题意:格路问题 没什么难度 难点在于如何快速计算相对较大的组合数 思路:运用手写计算组合数的方式进行计算  如c(8,3) 如果手算就是   8*7*6/(3*2*1)这样可以很快得解出 计算代码为: ...

  5. Gym - 101775A Chat Group 组合数+逆元+快速幂

    It is said that a dormitory with 6 persons has 7 chat groups ^_^. But the number can be even larger: ...

  6. HDU 4704 Sum(隔板原理+组合数求和公式+费马小定理+快速幂)

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description   Sample Input 2 Sample Outp ...

  7. 【BZOJ4870】组合数问题(动态规划,矩阵快速幂)

    [BZOJ4870]组合数问题(动态规划,矩阵快速幂) 题面 BZOJ 洛谷 题解 显然直接算是没法做的.但是要求的东西的和就是从\(nk\)个物品中选出模\(k\)意义下恰好\(r\)个物品的方案数 ...

  8. [BZOJ3209]花神的数论题 组合数+快速幂

    3209: 花神的数论题 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2498  Solved: 1129[Submit][Status][Disc ...

  9. 牛客网 牛客小白月赛1 I.あなたの蛙が帰っています-卡特兰数,组合数阶乘逆元快速幂

    I.あなたの蛙が帰っています   链接:https://www.nowcoder.com/acm/contest/85/I来源:牛客网     这个题有点意思,是卡特兰数,自行百度就可以.卡特兰数用处 ...

随机推荐

  1. JavaWeb学习总结-05 Servlet 与页面的交互(02)

    一 模拟请求数据 为了测试方便,把请求 json,txt, xml,html格式的文件放到了公网上面,可以通过以下地址请求: http://wx.glab.cn/xpxiaowu4java/json/ ...

  2. UVA1210Sum of Consecutive Prime Numbers(素数打表 + 连续和)

    题目链接 题意:输入一个数n (2 <= n <= 10000) 有多少种方案可以把n写成若干个连续素数之和 打出10000之内的素数表,然后再打出每个可能得到的和的方案数的表 #incl ...

  3. 网站缓存数据到tomcat服务器

    通过缓存使相同的数据不用重复加载,降低数据库的访问 public class CacheFilter implements Filter { //实例变量[每线程共享] private Map< ...

  4. confluence安装

    confluence安装 1.jre安装 java下载http://www.java.com/zh_CN/download/manual.jsp 创建目录和解压缩 mkdir -p /usr/loca ...

  5. 线性回归和批量梯度下降法python

    通过学习斯坦福公开课的线性规划和梯度下降,参考他人代码自己做了测试,写了个类以后有时间再去扩展,代码注释以后再加,作业好多: import numpy as np import matplotlib. ...

  6. c#.net WinForm 线程内 调用窗体控件

    richTextBox1.BeginInvoke(new EventHandler(delegate { richTextBox1.AppendText("正在提交服务器..\r\n&quo ...

  7. MSMQ 学习(1)

    在 Windows Server 2008 or Windows Server 2008 R2 上安装消息队列 4.0 在服务器管理器中,单击“功能”. 在“功能摘要”下的右窗格中,单击“添加功能”. ...

  8. Win7 配置Apache+PHP+Mysql环境

    第一.安装并配置APACHE(安装到D:\phpapache\Apache2.2) 1.安装时默认安装,Network Domain, Server Name 我填写我的计算机名,Administra ...

  9. 史上最全的Linux常用命令

    系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...

  10. Java——基本容器:JFrame

    创建一个新的窗体 import java.awt.Color; import javax.swing.JFrame; //======================================= ...