hdu5396 Expression 记忆化搜索+组合数 多校联合第九场

Expression

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 160    Accepted Submission(s): 90


Problem Description
Teacher Mai has n numbers a1,a2,,anand n1 operators("+", "-" or "*") op1,op2,,opn1, which are arranged in the form a1 op1 a2 op2 a3  an.

He wants to erase numbers one by one. In i-th round, there are n+1i 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 n1 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+4683" is 1+46831+4(2)31+(8)3(7)321.
 

Input
There are multiple test cases.

For each test case, the first line contains one number n(2n100).

The second line contains n integers a1,a2,,an(0ai109).

The third line contains a string with length n1 consisting "+","-" and "*", which represents the operator sequence.
 

Output
For each test case print the answer modulo 109+7.
 

Sample Input
 
   
3 3 2 1 -+ 5 1 4 6 8 3 +*-*
 

Sample Output
 
  
2 999999689
Hint
Two numbers are considered different when they are in different positions.

  一个表达式,加不同的括号得到不同的计算顺序,只要有一个计算顺序不同这两个表达式就是不同的。求所有表达式的和。

  用dp[l][r]表示第l个数到第r个数组成的各种顺序的表达式和是多少,t[l][r]表示第l个数到第r个数有多少种不同的组合。dp[l][r]的计算方法是枚举最后一个被计算的位置i,设n1=dp[l][i],n2=dp[i+1][r],t1=t[l][i],t2=t[i+1][r]。那么对于加号,对于每个i要加上n1*t2+n2*t1,对于右边不同的组合,左边的数每次都要被加一次,同理左边不同的组合,右边的数每次也要被加一次。因此n1被加了t2次,n2被加了t1次。减法和加法一样。乘法是直接n1*n2。这还没完,注意就算是左边的顺序和右边的顺序的确定,假设左边有f1个符号,右边有f2个符号,也有C[f1+f2][f1]种排法,相当于在f1+f2个位置中选f1个,剩下的给f2,f1和f2中排列的相对顺序不改变,所以还要乘上C[f1+f2][f1]。同理对于每个i,t[l][r]要加上t1*t2*C[f1+f2][f1]。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long LL;

const int MAXN=110;
const int INF=0x3f3f3f3f;
const LL MOD=1e9+7;

int T,N;
int a[MAXN];
LL dp[MAXN][MAXN],t[MAXN][MAXN],C[MAXN][MAXN];
char str[MAXN];

void getC(){
    for(int i=0;i



你可能感兴趣的:(DP)