题目链接
(Solution)
我们考虑每增加一个(1)会对答案有什么影响:
[E((x+1)^3)-E(x^3)=E(3x^2+3x+1)=3E(x^2)+3E(x)+1
]
所以我们只需要维护(E(x^2))和(E(x))
令:
(x1[i]=E(x))
(x2[i]=E(x^2))
(x1[i]=(x1[i-1]+1)*p[i])
(x2[i]=(x2[i-1]+x1[i-1]*2+1)*p[i])
(ans[i]=ans[i-1]+(x2[i-1]*3+x1[i-1]*3+1)*p[i])
然后写的时候可以把数组省掉
(Code)
#include<cstdio>
double p,x1,x2,ans;
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lf",&p),ans+=(x2*3+x1*3+1)*p,x2=p*(x2+2*x1+1),x1=(x1+1)*p;
printf("%0.1lf",ans);
return 0;
}