2015暑假多校联合---Expression(区间DP)

题目链接

http://acm.split.hdu.edu.cn/showproblem.php?pid=5396

Problem Description

Teacher Mai has n numbers a1,a2,?,anand n−1 operators("+", "-" or "*")op1,op2,?,opn−1, 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+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.

Input

There are multiple test cases.

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.

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

2015 Multi-University Training Contest 9

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5867 5866 5865 5864 5863

题意:输入n个数,n-1个运算符(只有“+”、“—”、“*”),组成一个算式,给这n-1个运算符赋予不同的运算优先级(运算先后次序),每次得到一个值,求在所有值的和;

思路:区间DP,对于区间[i,j] 分为[i,k] 和[k+1,j]   设[i,k]在不同运算次序下的所有值为X1、X2、X3....Xn  那么dp[i][k]=(X1+X2+X3+...+Xn)

同样设dp[k+1][j]=(Y1+Y2+Y3+....+Ym)     如果第k个运算符为“*”  dp[i][j]=X1*(Y1+...+Ym)+...+Xn*(Y1+...+Ym) =dp[i][k]*dp[k+1][j];

如果不是“*”    dp[i][j]=dp[i][k]*A[j-1-k]+dp[k+1][j]*A[k-i]    A[]表示排列,为什么要乘以排列数呢? 分析可知,对于区间[i,k]中的一个值对应区间[k+1][j]的所有         值,而后面区间中由运算符优先级得到的值的个数就是后面区间的运算符个数的排列数;

最后要乘上组合数,对于区间[i,j]分为[i,k] [k+1,j]的贡献次数是C[j-i][k-i] 为什么呢?  因为前后两个区间的运算优先级对彼此没有影响;

代码如下:

#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+7;
long long a[105];
char s[105];
long long dp[105][105];
long long A[120];
long long C[120][120];

int main()
{
    int n;
    A[0]=1;
    for(long long i=1;i<=110;i++)  ///排列数;
        A[i]=A[i-1]*i%mod;

    for(int i=0;i<110;i++)         ///组合数;
        C[i][0]=1;
    C[1][1]=1;
    for(int i=1;i<110;i++)
    {
        for(int j=1;j<=i;j++)
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
    }
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        scanf("%s",s+1);
        memset(dp,0,sizeof(dp));

        for(int i=1;i<=n;i++)
            dp[i][i]=a[i];

        for(int len=2;len<=n;len++)
        {
            for(int i=1;i<=n;i++)
            {
                if(i+len-1>n) break;
                for(int k=i;k<i+len-1;k++)
                {
                    long long t;
                    if(s[k]==‘*‘)
                        t=(dp[i][k]*dp[k+1][i+len-1])%mod;
                    else if(s[k]==‘-‘)
                        t=(dp[i][k]*A[i+len-2-k]-dp[k+1][i+len-1]*A[k-i])%mod;
                    else
                        t=(dp[i][k]*A[i+len-2-k]+dp[k+1][i+len-1]*A[k-i])%mod;
                    dp[i][i+len-1]=(dp[i][i+len-1]+t*C[len-2][k-i])%mod;
                }
            }
        }
        printf("%lld\n",(dp[1][n]%mod+mod)%mod);
    }
    return 0;
}
时间: 2024-12-04 12:33:19

2015暑假多校联合---Expression(区间DP)的相关文章

2015暑假多校联合---Mahjong tree(树上DP 、深搜)

题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artist. Today he is playing mahjong alone. He suddenly feels that the tree in the yard doesn't look good. So he wants to decorate the tree.(The tree has n

2015暑假多校联合---CRB and His Birthday(01背包)

题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son.She went to the nearest shop with M Won(currency unit).At the shop, there are N kinds of pr

2015暑假多校联合---Friends(dfs枚举)

原题链接 Problem Description There are n people and m pairs of friends. For every pair of friends, they can choose to become online friends (communicating using online applications) or offline friends (mostly using face-to-face communication). However, e

2016暑假多校联合---Rikka with Sequence (线段树)

2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has an array A with n numbers. Then he make

2016暑假多校联合---Another Meaning

2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two meanings. Such as “hehe”, which not only means “hehe”, but also means “excuse me”. Today, ?? is chating with MeiZi online, MeiZi sends a sentence A to

2016暑假多校联合---Windows 10

2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on the top of a mountain. Recently, our old monk found the operating system of his computer was updating to windows 10 automatically and he even can't j

2016暑假多校联合---Substring(后缀数组)

2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a string, he has to calculate the total number of its distinct substrings. But ?? thinks that is too easy, he wants to make this problem more interesti

hdu 5396 Expression(区间dp)

Problem Description Teacher Mai has n numbers a1,a2,?,anand n−1 operators("+", "-" or "*")op1,op2,?,opn−1, 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+

[hdu5396 Expression]区间DP

题意:给一个表达式,求所有的计算顺序产生的结果总和 思路:比较明显的区间dp,令dp[l][r]为闭区间[l,r]的所有可能的结果和,考虑最后一个符号的位置k,k必须在l,r之间,则l≤k<r,dp[l][r]=Σ{dp[l][k]?dp[k+1][r]}*(r-l-1)!/[(k-l)!(r-k-1)!],其中(r-l-1)!/[(k-l)!(r-k-1)!]表示从左区间和右区间选择符号的不同方法总数(把左右区间看成整体,那么符号的选择在整体间也有顺序,内部的顺序不用管,那是子问题需要考虑的)