每多一个1带来的贡献增值是:(x+1)^3-x^3=3*x^2+3*x+1,这里的x是前面已经累计的连续1的个数,而对于每一位的期望贡献就是p[i]*E(3*x^2+3*x+1)=p[i]*(3E(x^2)+3E(x)+1,在累加即得答案,所以这一要求得每一位的E(x^2)与E(x),
有递推:
x=x-1+1
E(x)=(E(x-1)+1)*p[i]
x^2=(x-1)^2+2*(x-1)+1
E(x^2)=(E((x-1)^2)+2*E(x-1)+1)*p[i]
写出一般等式,在求两边期望(这个取定1要补充个概率)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int nn=1e5+50;
inline int read()
{
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
inline void write(int x)
{
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
vector<pair<int,int>>a[nn];
queue<int>q;
double f[nn],g[nn];
double p[nn];
double ans[nn];
int main()
{
int n;
n=read();
for(int i=1;i<=n;++i)
{
scanf("%lf",&p[i]);
}
for(int i=1;i<=n;++i)
{
f[i]=(f[i-1]+1.0)*p[i];
}
for(int i=1;i<=n;++i)
{
g[i]=(g[i-1]+2.0*f[i-1]+1.0)*p[i];
}
for(int i=1;i<=n;++i)
{
ans[i]=ans[i-1]+(3.0*g[i-1]+3.0*f[i-1]+1.0)*p[i];
}
ans[n]*=10;
ans[n]=round(ans[n]);
ans[n]/=10.0;
printf("%.1lf",ans[n]);
return 0;
}