POJ 3682 King Arthur's Birthday Celebration

版权声明:写得不好,转载请通知一声,还请注明出处,感激不尽 https://blog.csdn.net/As_A_Kid/article/details/82620641

Problem

POJ
正面的概率为p,掷k次正面就停止,求期望掷多少次。

Solution

设f[i]表示掷i次的期望

f [ i ] = 1 + p f [ i + 1 ] + ( 1 p ) f [ i ]

f [ i ] = 1 p + f [ i + 1 ]

f [ k ] = 0 f [ i ] = k i p

可得 f [ 0 ] = k p


设g[i]表示对天数加权后掷i次的期望

g [ i ] = p g [ i + 1 ] + ( 1 p ) g [ i ] + 2 f [ i ] 1

g [ i ] = g [ i + 1 ] + 2 ( k i ) p 2 1 p

上求和公式可得 g [ 0 ] = k ( k p + 1 ) p 2

但是如果代价是一个与天数有关的二阶(或更高)的函数关系,那么就不能直接写成 + f [ i ] 2 了。因为期望可以理解为是平均情况下的代价,而平均情况的平方和先平方再求平均是不一样的。

不知道为什么语言选G++就WA,C++才A的。。

Code

#include <cstdio>
#define rg register
using namespace std;
typedef long long ll;
template <typename Tp> inline int getmin(Tp &x,Tp y){return y<x?x=y,1:0;}
template <typename Tp> inline int getmax(Tp &x,Tp y){return y>x?x=y,1:0;}
template <typename Tp> inline void read(Tp &x)
{
    x=0;int f=0;char ch=getchar();
    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    if(ch=='-') f=1,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    if(f) x=-x;
}
int k;
double p;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    while(~scanf("%d",&k)&&k)
    {
        scanf("%lf",&p);
        printf("%.3lf %.3lf\n",k/p,1.0*k*(k+1.0-p)/p/p);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/As_A_Kid/article/details/82620641