版权声明:写得不好,转载请通知一声,还请注明出处,感激不尽 https://blog.csdn.net/As_A_Kid/article/details/82620641
Problem
POJ
正面的概率为p,掷k次正面就停止,求期望掷多少次。
Solution
设f[i]表示掷i次的期望
可得
设g[i]表示对天数加权后掷i次的期望
上求和公式可得
但是如果代价是一个与天数有关的二阶(或更高)的函数关系,那么就不能直接写成 了。因为期望可以理解为是平均情况下的代价,而平均情况的平方和先平方再求平均是不一样的。
不知道为什么语言选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;
}