题解
设
f[i]
f
[
i
]
为以
i
i
结尾的后缀1长度
为以
i
i
结尾的后缀1长度的平方
为以
i
i
的答案
因为
(x+1)2=x2+2x+1
(
x
+
1
)
2
=
x
2
+
2
x
+
1
,可知
g[i]=(g[i−1]+2∗f[i−1]+1)∗p[i]
g
[
i
]
=
(
g
[
i
−
1
]
+
2
∗
f
[
i
−
1
]
+
1
)
∗
p
[
i
]
因为
(x+1)3=x3+3x2+3x+1
(
x
+
1
)
3
=
x
3
+
3
x
2
+
3
x
+
1
,可知
h[i]=(h[i−1]+3∗g[i−1]+3∗f[i−1]+1)∗p[i]+h[i−1]∗(1−p[i])
h
[
i
]
=
(
h
[
i
−
1
]
+
3
∗
g
[
i
−
1
]
+
3
∗
f
[
i
−
1
]
+
1
)
∗
p
[
i
]
+
h
[
i
−
1
]
∗
(
1
−
p
[
i
]
)
(h为所求,因此要加上以0结尾的答案)
代码
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 100010
int n,T;
double f[N],g[N],h[N],p[N];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lf",&p[i]);
f[i]=(f[i-1]+1)*p[i];
g[i]=(g[i-1]+2*f[i-1]+1)*p[i];
h[i]=(h[i-1]+3*g[i-1]+3*f[i-1]+1)*p[i]+h[i-1]*(1-p[i]);
}
printf("%.1lf",h[n]);
return 0;
}