【多校第9场】【组合数学】【区间dp】【Expression】

Expression

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


Problem Description
Teacher Mai has  n  numbers  a1,a2,,an and  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.
 

Author
xudyh
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5405  5404  5403  5402  5401 




#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <string>
#include <queue>
#include <bitset>
using namespace std;

int n;
const int N = 210;
const int MOD = 1e9 + 7;
typedef long long ll;
int a[N];
char op[N];
ll dp[N][N];
ll comb[N][N];
ll fac[N];
void init()
{
    comb[0][0] = 1;

    for(int i=0;i<=101;i++)
    {
        comb[0][i] = 0;
        comb[i][0] = 1;
        for(int j=1;j<=i;j++)
    comb[i][j] = (comb[i-1][j] + comb[i-1][j-1]) % MOD;
    }
    fac[0] = 1;
    for(int i=1;i<=101;i++) fac[i] = fac[i-1] * i % MOD;



}


int main()
{
    init();

    while(scanf("%d",&n) != EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        scanf("%s",op);
        for(int j=0;j<n;j++) {
            dp[j][j] = a[j];
            for(int i=j-1;i>=0;i--) {
                dp[i][j] = 0;
                for(int k=i;k<j;k++) {

                 if(op[k] == '*') {
                    dp[i][j] = (dp[i][j] + (dp[i][k] * dp[k+1][j]) % MOD * comb[j-i-1][k-i]) % MOD;
                 }
                 else if(op[k] == '+') {
                    dp[i][j] = (dp[i][j] + (dp[i][k] * fac[j-k-1] + dp[k+1][j] * fac[k-i]) % MOD * comb[j-i-1][k-i]) % MOD;
                 }
                 else
                 {
                     dp[i][j] = (dp[i][j] + (dp[i][k] * fac[j-k-1] - dp[k+1][j] * fac[k-i]) % MOD * comb[j-i-1][k-i]) % MOD;
                 }
                }
            }
        }
        if(dp[0][n-1] < 0) dp[0][n-1] += MOD;
        printf("%I64d\n",dp[0][n-1]);
    }
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值