HDU 4465 Candy

来源:互联网 发布:zookeeper java 实例 编辑:程序博客网 时间:2024/05/18 00:08

Candy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1908    Accepted Submission(s): 833
Special Judge


Problem 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 × 105) 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.400000100 0.500000124 0.432650325 0.325100532 0.4875202276 0.720000
 

Sample Output
Case 1: 3.528175Case 2: 10.326044Case 3: 28.861945Case 4: 167.965476Case 5: 32.601816Case 6: 1390.500000
 

Source
2012 Asia Chengdu Regional Contest


题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=4465

题目大意 :两个盒子开始都有n块糖,每次从一个中拿糖的概率为p,求当一个盒子空时,另一个盒子中糖的数目期望

题目分析 :不难得到公式: ans = 求和(i = 0~n)(n-i)  * C(i, n + i) * ( (p ^ n* (1 - p) ^ i) + (p ^ i + (1 - p) ^ n)),本题n上限为20000,组合数存不下故中间过程要用递推公式即an = an * n / (n + 1) *(n + i) / i * p这样还是会卡精度有点坑爹,可以采用一个很简单的技巧。见程序

#include <cstdio>#include <cmath>int main(){    int n, ca = 0;    double p;    while(scanf("%d %lf", &n, &p) != EOF)    {        double tmp1 = n, tmp2 = n ;        // printf("tmp1 = %f  tmp2 = %f\n", tmp1, tmp2);        double ans1 = tmp1 * pow(1 - p, n + 1);        double ans2 = tmp2 * pow(p, n + 1);        int now = n;        int re = n + 1;        for(int i = 1; i < n; i++)        {            now = n - i; //系数            //递推公式            tmp1 *= 1.0  / (double)(now + 1) * p / i * (n + i) * now;            tmp2 *= 1.0  / (double)(now + 1) * (1 - p) / i * (n + i) * now;            //当tmp大于某个设定的值时让它乘p(或(1-p))来保证其数字不会暴精度            //关键技巧            while(tmp1 > n || tmp2 > n)            {                tmp1 *= (1 - p);                tmp2 *= p;                re--;            }            //因为每一项都要乘pow(1-p, n+1)或pow(p,n+1)            //我们在此补上剩余未乘的p(或(1-p))的幂数即可            ans1 += tmp1 * pow(1 - p, re);            ans2 += tmp2 * pow(p, re);           // printf("tmp1 = %lf   tmp2 = %lf  \n", tmp1, tmp2);        }        printf("Case %d: %lf\n", ++ca, ans1 + ans2);    }}




0 0
原创粉丝点击