• POJ3682 概率DP


    King Arthur's Birthday Celebration
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 3575   Accepted: 1130

    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 F[i]rst 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
    

    Source

    题意: 有一个富豪,他决定每天撒钱,并且抛硬币,第一天1块钱,第二天3块钱,第三天5块,直到他抛到硬币向上的数量为K。 求天数期望和钱期望。

    思路

    C[i]表示掷出了i枚正面朝上的硬币的期望次数,F[i]表示掷出了i枚正面朝上的硬币的期望费用。

    C[i] = pC[i]-1 + (1-p)C[i] + 1   1表示抛出了第i枚硬币用去1次,如果是正面那么要到i枚正面硬币只需要再抛出C[i]-1次,如果是反面还需要再抛C[i]次。

    F[i] = p(F[i]-1 + 2 * (C[i]-1+1) -1) + (1-p)(F[i] + 2 * (C[i]+1) -1)    第i枚硬币抛出,如果是正面,那么现在抛出的这枚就是第C[i]-1 + 1 枚,如果是反面,现在抛出的这枚就是第C[i] + 1 枚。

    PS:C[i],F[i]的转移方程都需要移项。

    代码:

     1 #include"bits/stdc++.h"
     2 
     3 #define db double
     4 #define ll long long
     5 #define vl vector<ll>
     6 #define ci(x) scanf("%d",&x)
     7 #define cd(x) scanf("%lf",&x)
     8 #define cl(x) scanf("%lld",&x)
     9 #define pi(x) printf("%d
    ",x)
    10 #define pd(x) printf("%f
    ",x)
    11 #define pl(x) printf("%lld
    ",x)
    12 #define rep(i, n) for(int i=0;i<n;i++)
    13 using namespace std;
    14 const int N   = 1e6 + 5;
    15 const int mod = 1e9 + 7;
    16 const int MOD = 998244353;
    17 const db  PI  = acos(-1.0);
    18 const db  eps = 1e-10;
    19 const ll INF = 0x3fffffffffffffff;
    20 
    21 db c[N];
    22 db f[N];
    23 int n;
    24 db p;
    25 int main()
    26 {
    27     while(scanf("%d%lf",&n,&p)==2&&n)
    28     {
    29         c[0]=f[0]=0;
    30         for(int i=1;i<=n;i++) c[i]=c[i-1]+1/p;
    31         for(int i=1;i<=n;i++) f[i]=(p*(f[i-1]+2*(c[i-1]+1)-1)+(1-p)*(2*(c[i]+1)-1))/p;
    32         printf("%.3f %.3f
    ",c[n],f[n]);
    33     }
    34     return 0;
    35 }
  • 相关阅读:
    cefsharp webBrowser Javascript 打开winForm界面
    大数据抓取采集框架(摘抄至http://blog.jobbole.com/46673/)
    Java对象序列化与RMI
    Java与编码问题串讲之二–如何理解java采用Unicode编码
    android中清空一个表---类似truncate table 表名 这样的功能 android sqlite 清空数据库的某个表
    使用Camera功能 AREA的理解
    去掉android的屏幕上的title bar
    Android View的onTouchEvent和OnTouch区别
    关于安卓HTTP请求用HttpUrlConnection还是HttpClient好
    如何确定拍照时,相机屏幕是横屏or竖屏?
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/9536125.html
Copyright © 2020-2023  润新知