题目链接
http://acm.split.hdu.edu.cn/showproblem.php?pid=5396
He wants to erase numbers one by one. In i-th round, there are n+1−i numbers remained. He can erase two adjacent numbers and the operator between them, and then put a new number (derived from this one operation) in this position. After n−1 rounds, there is the only one number remained. The result of this sequence of operations is the last number remained.
He wants to know the sum of results of all different sequences of operations. Two sequences of operations are considered different if and only if in one round he chooses different numbers.
For example, a possible sequence of operations for "1+4∗6−8∗3" is 1+4∗6−8∗3→1+4∗(−2)∗3→1+(−8)∗3→(−7)∗3→−21.
For each test case, the first line contains one number n(2≤n≤100).
The second line contains n integers a1,a2,⋯,an(0≤ai≤109).
The third line contains a string with length n−1 consisting "+","-" and "*", which represents the operator sequence.
Two numbers are considered different when they are in different positions.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#define eps 1e-8
#define maxn 105
#define inf 0x3f3f3f3f3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
const long long mod=1e9+;
long long a[];
char s[];
long long dp[][];
long long A[];
long long C[][]; int main()
{
int n;
A[]=;
for(long long i=;i<=;i++) ///排列数;
A[i]=A[i-]*i%mod; for(int i=;i<;i++) ///组合数;
C[i][]=;
C[][]=;
for(int i=;i<;i++)
{
for(int j=;j<=i;j++)
C[i][j]=(C[i-][j-]+C[i-][j])%mod;
}
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
scanf("%s",s+);
memset(dp,,sizeof(dp)); for(int i=;i<=n;i++)
dp[i][i]=a[i]; for(int len=;len<=n;len++)
{
for(int i=;i<=n;i++)
{
if(i+len->n) break;
for(int k=i;k<i+len-;k++)
{
long long t;
if(s[k]=='*')
t=(dp[i][k]*dp[k+][i+len-])%mod;
else if(s[k]=='-')
t=(dp[i][k]*A[i+len--k]-dp[k+][i+len-]*A[k-i])%mod;
else
t=(dp[i][k]*A[i+len--k]+dp[k+][i+len-]*A[k-i])%mod;
dp[i][i+len-]=(dp[i][i+len-]+t*C[len-][k-i])%mod;
}
}
}
printf("%lld\n",(dp[][n]%mod+mod)%mod);
}
return ;
}