Candy( 数学期望 )

Candy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1830    Accepted Submission(s): 794

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.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<cstdio>
#include<cstring>
# include<string>
# include<cmath>
using namespace std;
//typedef __int64  lld;
const int maxn=400010+5;
int n;
double q;
double p;
double c[maxn];
double C(int x, int y)
{
     return c[x]-c[y]-c[x-y];
}

double v1(int x )
{
     return    C(2*n-x,n)+(n+1)*log(q*1.0)+(n-x)*log(p*1.0);
}
double v2(int y)
{
    return   C(2*n-y,n) + (n+1)*log(p*1.0)+(n-y)*log(q*1.0);
}
int main()
{

      int k=0;
       c[0]=0;
       for( int i=1; i<400010; i++)
            c[i]=c[i-1]+log(i*1.0);

    while(cin>>n>>q)
    {
       k++;
       double tot=0;
         p=1-q;
        for(int  i=n; i>0;i--)
        {
            tot+=i*( exp(v1(i))+exp(v2(i)) );
        }

        printf("Case %d: %lf\n",k,tot);

    }
    return 0;
}

#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 400200
double f[N];
double logc(int n,int m)
{
    return f[m]-f[n]-f[m-n];
}
int main ()
{
    int n;
    int i,j,k=0;
    double p,ans,ret;
    f[0]=0;
    for (i=1;i<N;++i) f[i]=f[i-1]+log(1.0*i);
    while (scanf("%d%lf",&n,&p)!=EOF)
    {
        ans=0;
        double p1=log(p),p2=log(1-p);
        double q1=(n+1)*p1,q2=(n+1)*p2;
        for (i=0;i<n;++i)
        {
            ans+=((n-i)*exp(logc(n,n+i)+q1+i*p2));
            ans+=((n-i)*exp(logc(n,n+i)+q2+i*p1));
        }
        printf("Case %d: %.6lf\n",++k,ans);
    }
    return 0;
}
时间: 2024-10-11 10:36:19

Candy( 数学期望 )的相关文章

UVa 1639 Candy (数学期望+组合数学+高精度存储)

题意:有两个盒子各有n个糖,每次随机选一个(概率分别为p,1-p),然后吃掉,直到有一次,你打开盒子发现,没糖了! 输入n,p,求另一个盒子里糖的个数的数学期望. 析:先不说这个题多坑,首先要用long double来实现高精度,我先用的double一直WA,后来看了题解是用long double, 改了,可一直改不对,怎么输出结果都是-2.00000,搞了一晚上,真是无语,因为我输入输出数据类型是long double, 结果一直不对 ,可能是我的编译器是C89的吧,和C语言,输入输出格式不同

数学期望

数学期望又称均值(加权均值),例如 甲8环,9环,10环的概率分别为0.1,0.8,0.1,即权重,则加权均值为8*0.1+9*0.8+10*0.1=9:同理乙的加权均值为8.95 则甲的平均成绩优于乙 对于离散型随机变量 连续型随机变量

数学1——概率与数学期望

1.什么是数学期望? 数学期望亦称期望.期望值等.在概率论和统计学中,一个离散型随机变量的期望值是试验中每一次可能出现的结果的概率乘以其结果的总和. 这是什么意思呢?假如我们来玩一个游戏,一共52张牌,其中有4个A.我们1元钱赌一把,如果你抽中了A,那么我给你10元钱,否则你的1元钱就输给我了.在这个游戏中,抽中的概率是$\frac{1}{13} ( \frac{4}{52} ) $,结果是赢10元钱:抽不中概率是$\frac{12}{13}$,结果是亏1元钱.那么你赢的概率,也就是期望值是$-

poj3682:数学期望,O(1)做法附推导过程

这几天一直在磨蹭这题..第一个答案很容易,但在第二个答案我无法算出来了,于是只好求助于Zayin.Zayin又求助于我们年级里面的一个研究生数学老师..而现在终于算出来了,我看了看,自己也推出来几次了,先看题:) King Arthur's Birthday Celebration Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2921 Accepted: 926 Description King Arthur is an

HDUBoard Game Dice (数学期望)

推出的公式是M^x*x/N,大概意思是M^x*x这些种可能后一定会找出一个裁判,在除以N为数学期望. 可能和数学公式还有关系. #include<stdio.h> #include<math.h> __int64 gcd(__int64 a,__int64 b){ return b == 0 ? a : gcd(b, a%b); } int main() { __int64 i,j,n,m,t,x,a,b,temp,sum; scanf("%I64d",&

HDU 4405 飞行棋上的数学期望

突然发现每次出现有关数学期望的题目都不会做,就只能找些虽然水但自己还是做不出的算数学期望的水题练练手了 题目大意: 从起点0点开始到达点n,通过每次掷色子前进,可扔出1,2,3,4,5,6这6种情况,扔到几前进几,当然对应飞行通道可以通过x直达一点y,x<y,计算到达n点或超过n 点要扔色子的次数的数学期望 从某一点 i 扔完色子可到达 i+1,i+2,i+3,i+4,i+5,i+6这6个点,令dp[i]为到达末尾的数学期望 那么到达之后6个点的数学期望是一样的,那么dp[i]=dp[i+1]*

概率论,简要数学期望(转载)

概率论(https://ruanx.pw/post/%E6%A6%82%E7%8E%87%E8%AE%BA.html) 这东西并不难学.这片博客主要介绍离散概率.连续概率.期望与微积分…… 离散型概率入门 计算方法 首先,我们来讨论一个最原始的问题:抛一个质地均匀的硬币,抛中正面的几率是多大?显然50%50%. 那么问题加深一番:抛两个质地均匀的硬币,都抛中正面的几率是多大?显然25%25%. 进一步,抛nn个硬币,全都正面朝上的几率是0.5×0.5×0.5?=0.5n0.5×0.5×0.5?=

HDU3910(数学期望题,题目难懂)

Liang Guo Sha Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 590    Accepted Submission(s): 426 Problem Description Maybe you know “San Guo Sha”, but I guess you didn’t hear the game: “Liang Gu

[sdut]2623+[sdut]2878//四五届省赛中的两道数学期望

两道数学期望的题今天一起总结上来. 1.the number of steps(第四届省赛) 1 #include <iostream> 2 #include <string.h> 3 #include <iomanip> 4 using namespace std; 5 double dp[100][100]; 6 int n; 7 double a,b,c,d,e; 8 9 int main() 10 { 11 while(cin>>n&&

ZOJ3329-One Person Game(概率DP求数学期望)

One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There is a very simple and interesting one-person game. You have 3 dice, namelyDie1, Die2 and Die3. Die1 hasK1 faces. Die2 has K2 faces.Die3 has K3 faces. All the dic