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. redis 源代码分析(一) 内存管理

    一,redis内存管理介绍 redis是一个基于内存的key-value的数据库,其内存管理是很重要的,为了屏蔽不同平台之间的差异,以及统计内存占用量等,redis对内存分配函数进行了一层封装,程序中 ...

  2. DataGrid( 数据表格) 组件[7]

    本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...

  3. (一)《Java编程思想》学习——按位运算符、移位运算符

    (第三章) (一)按位运算符 按位逻辑运算符有: “与”(AND)        & 1&1=1;1&0=0;0&0=0 “或”(OR) | 1|1=1;1|0=1;0 ...

  4. SQL2008安装重启失败

    我今天安装SQL2008的一些问题经历SQL2008安装重启失败大致出错信息如下:RebootRequiredCheck 检查是否需要挂起计算机重新启动.挂起重新启动会导致安装程序失败. 失败 需要重 ...

  5. Android-为何以及如何保存Fragment实例

    在安卓开发中,由于旋转设备会造成配置改变进而导致Activity实例被摧毁(当然也包括Activity托管的Fragment).Activity或Fragment实例被摧毁自然也就让Model被摧毁, ...

  6. java日期操作

    //字符串转日期 public static void dt7() throws ParseException { String str_date="2015---08---08" ...

  7. math.h中的常量

    类似于Matlab中经常用到的一些常量,C++里边也是有的.(经查源文件无意中看到) 写入如下代码: #include<iostream> #include<iomanip> ...

  8. php目录分隔符DIRECTORY_SEPARATOR

    在windows我们习惯性的使用“\”作为文件分隔符,但是在linux上系统不认识这个标识,于是就要引入这个php内置变量了:DIRECTORY_SEPARATOR 路径分隔符 windows \ o ...

  9. Activiti工作流学习-----基于5.19.0版本(5)

    五.与Spring集成 实际项目中一般都有Spring的身影,与Spring集成使得Activiti的实用性得到提高.activiti和Spring整合需要activiti-spring的jar在类路 ...

  10. zookeeper笔记--配置以及和spark hbase结合使用

    Spark集群基于ZooKeeper的搭建:http://www.dataguru.cn/thread-333245-1-1.html Spark需要修改的地方: 进入spark的配置目录,参照下面代 ...