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
 

2015 Multi-University Training Contest 9

设d[i][j] 代表i~j的答案。区间DP枚举(i, j)区间的断点,如果断点处的操作符是‘*’,那么该区间的答案可以直接加上d[i][k] *  d[k+1][j],因为乘法分配律可以保证所有的答案都会乘起来。如果是加法,需要加的 就是 左边的答案 乘 右边操作数的阶乘 加上 右边的答案乘左边操作数的阶乘,最后要确定左边操作和右边操作的顺序 因为每个答案里是统计了该区间所有的阶乘情况,因此每一个左边已确定的顺序和右边已确定的顺序需要排列组合一下。比如:左边有3个操作数+-*,右边有2个操作符+-,当已经确定了他们各自的顺序,假设左边算-*+,右边+-,这个顺序已经固定,现在有五个操作符需要操作,我需要选择三个位置给左边的操作符-*+,那么右边的两个操作符自然就对应他们相应的位置。

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 106
#define MOD 1000000007
#define ll long long
ll A[N]={};
ll C[N][N]={};
ll dp[N][N];
char op[N];
void init()
{
A[]=;
for(ll i=;i<N;i++)
A[i]=A[i-]*i%MOD; for(ll i=;i<N;i++)
C[i][i]=C[i][]=;
for(int i=;i<N;i++)
{
for(ll j=;j<i;j++)
C[i][j]=(C[i-][j-]+C[i-][j])%MOD;
}
}
int main()
{
init();
ll n;
while(scanf("%I64d",&n)==)
{
memset(dp,,sizeof(dp));
for(ll i=;i<n;i++)
{
scanf("%I64d",&dp[i][i]);
}
scanf("%s",op); for(ll len=;len<=n;len++)
{
for(ll i=;i+len<n;i++)
{
ll j=i+len;
for(ll k=i;k<j;k++)
{
ll tmp;
if(op[k]=='+')
{
tmp=(dp[i][k]*A[j-k-]+dp[k+][j]*A[k-i])%MOD;
}
else if(op[k]=='-')
{
tmp=(dp[i][k]*A[j-k-]-dp[k+][j]*A[k-i])%MOD;
tmp=(tmp+MOD)%MOD;
}
else
{
tmp=(dp[i][k]*dp[k+][j])%MOD;
}
tmp=tmp*C[j-i-][k-i]%MOD;
dp[i][j]=(dp[i][j]+tmp+MOD)%MOD;
}
}
}
printf("%I64d\n",dp[][n-]);
}
return ;
}
 

hdu 5396 Expression(区间dp)的更多相关文章

  1. HDU 5396 Expression(DP+组合数)(详解)

    题目大意: 给你一个n然后是n个数. 然后是n-1个操作符,操作符是插入在两个数字之间的. 由于你不同的运算顺序,会产生不同的结果. 比如: 1 + 1 * 2 有两种  (1+1)*2   或者   ...

  2. You Are the One HDU - 4283 (区间DP)

    Problem Description The TV shows such as You Are the One has been very popular. In order to meet the ...

  3. Dire Wolf HDU - 5115(区间dp)

    Dire Wolf Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)Total ...

  4. [hdu5396 Expression]区间DP

    题意:给一个表达式,求所有的计算顺序产生的结果总和 思路:比较明显的区间dp,令dp[l][r]为闭区间[l,r]的所有可能的结果和,考虑最后一个符号的位置k,k必须在l,r之间,则l≤k<r, ...

  5. 2015 Multi-University Training Contest 9 hdu 5396 Expression

    Expression Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  6. HDU 5568 sequence2 区间dp+大数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5568 题意: 求所有长度为k的严格升序子序列的个数. 题解: 令dp[i][k]表示以i结尾的长度为 ...

  7. hdu 4579 博弈+区间dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 #include <cstdio> #include <cstring> ...

  8. hdu 5396 Expression

    考虑到此题麻烦了某hust大神&体现出了自己数学能力的欠缺 虽然最近一直比较忙 还是把这题的题解写下来吧 首先看完数据范围后 应该有不少人会反应到是$n^3$的DP 以$F[i][j]$表示从 ...

  9. hdu5396 Expression 区间dp +排列组合

    #include<stdio.h> #include<string> #include<map> #include<vector> #include&l ...

随机推荐

  1. CATransform3D 讲解

    最近看到一个3D动画效果,决定认真就研究一下.从网上看到很多介绍,下面详细的讲解一下CATransform3D CATransform3D结构成员的意义. structCATransform3D { ...

  2. Eclipse中设置编码的方式

    如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码.然而,Eclipse工 作空间(workspace)的缺省字符编码是操作系统缺省的编码, ...

  3. Codeforces Round #FF (Div. 2):Problem A - DZY Loves Hash

    A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. [Angular 2]ng-class and Encapsulated Component Style2

    Many Components require different styles based on a set of conditions. Angular 2 helps you style you ...

  5. Python发一个GET请求

    # -*- coding: utf-8 -*- try: import httplib2 except ImportError: print('错误:') print(' httplib2这个库没有找 ...

  6. Android原理揭秘系列之一动态墙纸

    Livewallpaper,即动态墙纸,是Android的一大3D特色功能,用户可以在桌面选择加载动态墙纸,让自己的手机桌面背景旋动起来. 相对于静态桌面壁纸,动态墙纸可以展示各种动态变化的背景,而与 ...

  7. c# winform InvokeRequired 解决跨线程访问控件

    C#中禁止跨线程直接访问控件,InvokeRequired是为了解决这个问题而产生的,当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它. Windows 窗体中 ...

  8. (原创)初识cordova(一)

    在公司做项目,发现有人在做大项目使用了cordova技术.做的是昆山的项目.之前听说过phonegap,也测试过,但是感觉效率不是很好,就没怎么研究,后来看他们做的项目还不错,于是想试一试. 搭建开发 ...

  9. (转) C# Activator.CreateInstance()方法使用

    C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...

  10. SignalR2.0开发实例之——设置时间、后台其他地方使用集线器、使用自己的连接ID

    一.连接的生命周期设置: 如下: // 该值表示连接在超时之前保持打开状态的时间长度. //默认为110秒 GlobalHost.Configuration.ConnectionTimeout = T ...