题目:
Expression
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 511 Accepted Submission(s): 300
Problem Description
Teacher Mai has
n numbers
a_1,a_2,\cdots,a_nand
n-1 operators("+", "-" or "*")
op_1,op_2,\cdots,op_{n-1}, which are arranged in the form
a_1~op_1~a_2~op_2~a_3~\cdots~a_n.
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\to 1+4*(-2)*3\to 1+(-8)*3\to (-7)*3\to -21.
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\to 1+4*(-2)*3\to 1+(-8)*3\to (-7)*3\to -21.
Input
There are multiple test cases.
For each test case, the first line contains one number n(2\leq n\leq 100).
The second line contains n integers a_1,a_2,\cdots,a_n(0\leq a_i\leq 10^9).
The third line contains a string with length n-1 consisting "+","-" and "*", which represents the operator sequence.
For each test case, the first line contains one number n(2\leq n\leq 100).
The second line contains n integers a_1,a_2,\cdots,a_n(0\leq a_i\leq 10^9).
The third line contains a string with length n-1 consisting "+","-" and "*", which represents the operator sequence.
Output
For each test case print the answer modulo
10^9+7.
Sample Input
3 3 2 1 -+ 5 1 4 6 8 3 +*-*
Sample Output
2 999999689HintTwo numbers are considered different when they are in different positions.
Author
xudyh
Source
Recommend
wange2014
题意:给n个数字n-1个操作,问所有的操作序列能够得到的表达式的值的和是多少
思路:区间DP,设dp[i][j]为i到j能够得到的所有表达式的和,枚举进行的操作,那么对于乘法操作,假设在位置k,那么由于乘法有分配律,我们把dp【i】【k】和dp【k+1】【j】乘起来就得到左边的所有表达式的和乘以右边的所有表达式的和的结果。(a1+a2+。。+ai)*(b1+b2+。。+bj)=a1*b1+a1*b2+。。。;对于加法操作,左边的每个表达式实际上要跟右边加A【j-k-1】次(A为排列,因为右边有j-k-1个操作),同理右边的每个表达式要跟左边加A【k-i】次;减法操作跟加法同理。得到的结果还要再乘上C[j-i][k-s],因为左右两边的顺序没有确定。
代码
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}
/*#ifdef HOME
freopen("in.txt","r",stdin);
#endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME
int Scan()
{
int res = 0, ch, flag = 0;
if((ch = getchar()) == '-') //判断正负
flag = 1;
else if(ch >= '0' && ch <= '9') //得到完整的数
res = ch - '0';
while((ch = getchar()) >= '0' && ch <= '9' )
res = res * 10 + ch - '0';
return flag ? -res : res;
}
/*--o--------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/
const int mod=1e9+7;
int a[105];
char op[105];
long long int dp[105][105];
long long int fact[105];
long long int c[105][105];
int main()
{int n;
fact[0]=1;
for(int i=1;i<=100;i++)
fact[i]=(fact[i-1]*i)%mod;
c[0][0]=1;
for(int i=0;i<=100;i++)
{
for(int j=0;j<=i;j++)
if(j==0||i==j)
c[i][j]=1;
else
c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
}
while(RI(n)!=EOF)
{
for(int i=0;i<n;i++)
RI(a[i]);
scanf("%s",op);
MS0(dp);
for(int i=0;i<n;i++)
dp[i][i]=a[i];
for(int len=2;len<=n;len++)
for(int s=0;s+len-1<n;s++)
{for(int k=s;k<s+len-1;k++)
{ long long tmp;
if(op[k]=='*')
tmp=(dp[s][k]*dp[k+1][s+len-1])%mod;
else
if(op[k]=='+')
tmp=(dp[s][k]*fact[s+len-2-k]+dp[k+1][s+len-1]*fact[k-s])%mod;
else
tmp=(dp[s][k]*fact[s+len-2-k]-dp[k+1][s+len-1]*fact[k-s])%mod;
tmp=(tmp+mod)%mod;
dp[s][s+len-1]=((dp[s][s+len-1]+tmp*c[len-2][k-s])%mod+mod)%mod;
}
}
printf("%I64d\n",dp[0][n-1]);
}
return 0;
}