zoukankan      html  css  js  c++  java
  • POJ King Arthur's Birthday Celebration

    POJ King Arthur's Birthday Celebration

    POJ传送门

    Description

    King Arthur is an narcissist who intends to spare no coins to celebrate his coming K-th birthday. The luxurious celebration will start on his birthday and King Arthur decides to let fate tell when to stop it. Every day he will toss a coin which has probability p that it comes up heads and 1-p up tails. The celebration will be on going until the coin has come up heads for K times. Moreover, the king also decides to spend 1 thousand coins on the first day's celebration, 3 thousand coins on the second day's, 5 thousand coins on the third day's ... The cost of next day will always be 2 thousand coins more than the previous one's. Can you tell the minister how many days the celebration is expected to last and how many coins the celebration is expected to cost?

    Input

    The input consists of several test cases.
    For every case, there is a line with an integer K ( 0 < K ≤ 1000 ) and a real number p (0.1 ≤ p ≤ 1).
    Input ends with a single zero.

    Output

    For each case, print two number -- the expected number of days and the expected number of coins (in thousand), with the fraction rounded to 3 decimal places.

    Sample Input

    1 1
    1 0.5
    0
    

    Sample Output

    1.000 1.000
    2.000 6.000
    

    题目翻译:

    一个亚瑟,天天抛硬币,这是一枚神奇的硬币,它正面朝上的概率是p,亚瑟一天抛一次硬币,并且他第i天会向外发2i-1个硬币。那么求,亚瑟抛K次向上的硬币的期望天数和发的期望钱数。


    题解:

    期望DP入门题,关于期望DP,请走:

    浅谈期望DP

    按照期望DP的一般原则,是刷表法倒推。但是这道题倒推比较难,所以我们按正着想,也就是期望DP设计状态的第二原则:直接按题目意义设。

    所以我们设置:(f[i])表示掷i次正面朝上的期望天数。其转移方程就是:(f[i]=p imes f[i-1]+(1-p) imes f[i]+1)

    化简自行解决。

    由上,再设置(dp[i])表示掷i次正面朝上的期望钱数。其转移方程就是:

    [dp[i]=p imes(dp[i-1]+2 imes(f[i-1]+1)-1)+(1-p) imes(dp[i]+2 imes(dp[i]+1)-1) ]

    即有AC代码:

    注意,POJ不能用lf输出。

    代码:

    #include<cstdio>
    using namespace std;
    const int maxn=1010;
    int k;
    double p;
    double f[maxn],dp[maxn];
    int main()
    {
        while(~scanf("%d",&k)&&k)
        {
            f[0]=dp[0]=0;
            scanf("%lf",&p);
            for(int i=1;i<=k;i++) 
                f[i]=f[i-1]+1/p;
            for(int i=1;i<=k;i++)
                dp[i]=dp[i-1]+2*f[i-1]-2*f[i]+(1+2*f[i])/p;
            printf("%.3f %.3f
    ",f[k],dp[k]);
        }
        return 0;
    }
    
  • 相关阅读:
    Android开发——弹性滑动的两种实现方式
    管理知识和解决信息爆炸问题的4种方法
    京东金融的业务版图
    京东金融的业务版图
    虚幻引擎4艺术大师
    Android开发——View滑动的三种实现方式
    Android开发之Path类使用详解,自绘各种各样的图形!
    C# Dictionary的遍历理解
    我想走全产业链发展路线
    Androd安全——混淆技术完全解析
  • 原文地址:https://www.cnblogs.com/fusiwei/p/13840483.html
Copyright © 2011-2022 走看看