洛谷P1654OSU!
思路:
利用期望的性质:
E
(
X
+
Y
)
=
E
(
X
)
+
E
(
Y
)
E(X+Y)=E(X)+E(Y)
E(X+Y)=E(X)+E(Y)
E
(
C
)
=
C
,
C
为
常
数
E(C)=C,C为常数
E(C)=C,C为常数
设连续
i
i
i个数为
1
1
1,贡献的期望为
E
(
i
3
)
E(i^3)
E(i3)。可得:
E
(
i
)
=
(
E
(
i
−
1
)
+
1
)
×
p
i
E(i)=(E(i-1)+1)\times p_i
E(i)=(E(i−1)+1)×pi
E
(
i
2
)
=
(
E
(
(
i
−
1
)
2
)
+
2
E
(
i
−
1
)
+
1
)
×
p
i
E(i^2)=(E((i-1)^2)+2E(i-1)+1)\times p_i
E(i2)=(E((i−1)2)+2E(i−1)+1)×pi
E
(
i
3
)
=
(
E
(
(
i
−
1
)
3
)
+
3
E
(
(
i
−
1
)
2
)
+
3
E
(
i
−
1
)
+
1
)
×
p
i
E(i^3)=(E((i-1)^3)+3E((i-1)^2)+3E(i-1)+1)\times p_i
E(i3)=(E((i−1)3)+3E((i−1)2)+3E(i−1)+1)×pi
设
d
p
k
,
i
dp_{k,i}
dpk,i为前
i
i
i个数的贡献期望,
k
k
k表示连续个数为
X
X
X时贡献为
X
k
X^k
Xk,可得:
d
p
1
,
i
=
E
(
i
)
=
(
d
p
1
,
i
−
1
+
1
)
×
p
i
dp_{1,i}=E(i)=(dp_{1,i-1}+1)\times p_i
dp1,i=E(i)=(dp1,i−1+1)×pi
d
p
2
,
i
=
E
(
i
2
)
=
(
d
p
2
,
i
−
1
+
2
×
d
p
1
,
i
−
1
+
1
)
×
p
i
dp_{2,i}=E(i^2)=(dp_{2,i-1}+2\times dp_{1,i-1}+1)\times p_i
dp2,i=E(i2)=(dp2,i−1+2×dp1,i−1+1)×pi
d
p
3
,
i
=
E
(
i
3
)
+
d
p
3
,
i
−
1
×
(
1
−
p
i
)
=
(
E
(
(
i
−
1
)
3
)
+
3
E
(
(
i
−
1
)
2
)
+
3
E
(
i
−
1
)
+
1
)
×
p
i
+
d
p
3
,
i
−
1
×
(
1
−
p
i
)
=
d
p
3
,
i
−
1
+
(
3
×
d
p
2
,
i
−
1
+
3
×
d
p
1
,
i
−
1
+
1
)
×
p
i
\begin{aligned}dp_{3,i}&=E(i^3)+dp_{3,i-1}\times(1-p_i)\\&=(E((i-1)^3)+3E((i-1)^2)+3E(i-1)+1)\times p_i+dp_{3,i-1}\times(1-p_i)\\&=dp_{3,i-1}+(3\times dp_{2,i-1}+3\times dp_{1,i-1}+1)\times p_i\end{aligned}
dp3,i=E(i3)+dp3,i−1×(1−pi)=(E((i−1)3)+3E((i−1)2)+3E(i−1)+1)×pi+dp3,i−1×(1−pi)=dp3,i−1+(3×dp2,i−1+3×dp1,i−1+1)×pi
输出
d
p
3
,
n
dp_{3,n}
dp3,n即可。
代码:
#include<bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
#define cl(x,y) memset(x,y,sizeof(x))
#define loop(x,y,z) for(x=y;x<=z;x++)
#define reve(x,y,z) for(x=y;x>=z;x--)
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define lson x<<1,l,mid
#define rson x<<1|1,mid+1,r
#define INF 1e18
const int N=1e6+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const double eps=1e-8;
const double pi=acos(-1);
using namespace std;
double dp[5][N],p[N];
int main()
{
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%lf",&p[i]);
for(i=1;i<=n;i++)
{
dp[1][i]=(dp[1][i-1]+1)*p[i];
dp[2][i]=(dp[2][i-1]+2*dp[1][i-1]+1)*p[i];
dp[3][i]=dp[3][i-1]+(3*dp[2][i-1]+3*dp[1][i-1]+1)*p[i];
}
printf("%.1lf\n",dp[3][n]);
return 0;
}