30 pts
枚举每一种情况,求答案,最后求和
时间复杂度O(2^n *n)
Another 30 pts
发现:有x个乘号,答案就是n-x
有x个乘号的方案数为C(n, x)
用杨辉三角预处理组合数就好
时间复杂度O(n^2)
90 pts
用f[i]表示前 i 个数字的答案
枚举距离 i 最近的加号前的数字 j
f[i] = ∑(f[j] + 2^(j-1) * (a[j+1] * … * a[i])
100 pts
用p[i]表示a[1] * … * a[i]
f[i] = ∑(f[j] +2^(j-1)*(p[i]/p[j])
f[i] = ∑f[j] + (∑2^(j-1) * p[j]^(-1))*p[i]
代码
#include<cstdio>
const int N = 100000 + 5;
const int mod = 1000000007;
int n, sum, sum1, f[N], p[N], a[N];
inline int pow(int a, int p) {//快速幂
int r = 1;
for(; p; p>>=1) {
if(p & 1)
r = 1ll*r*a % mod;
a = 1ll*a*a % mod;
}
return r;
}
int main() {
scanf("%d", &n);
sum = 0;
sum1 = 1;
p[0] = 1;
for(int i = 1; i <= n; ++i) {
scanf("%d", a+i);
p[i] = 1ll*p[i-1]*a[i] % mod;//rt,p数组
f[i] = (sum+1ll*p[i]*sum1) % mod;//rt,f数组
sum = (sum+f[i]) % mod;
sum1 = (sum1+1ll*pow(2, i-1)*pow(p[i], mod-2)) % mod;
}
printf("%d\n", f[n]);
return 0;
}