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. linux下解压压缩rar文件

    http://download.csdn.net/detail/hnust_xiehonghao/6679893   下载地址 1. 下载软件 以rarlinux-3.8.0 for linux为例, ...

  2. Android ListView 滚动的N种方法

    Android 里面让ListView滚动有N种方法,这儿列举三种: 我的需求是通过按键让Listview滚动起来,当然这些按键不是通过Android标识接口传输过来的,所以不能通过监听按键事件来实现 ...

  3. Android摇一摇振动效果Demo

    前言     在微信刚流行的时候,在摇一摇还能用来那啥的时候,我也曾深更半夜的拿着手机晃一晃.当时想的最多的就是.我靠,为神马摇一下须要用这么大的力度,当时我想可能腾讯认为那是个人性的设计.后来才发觉 ...

  4. Android 屏幕适配方案

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/45460089: 本文出自:[张鸿洋的博客] 1.概述 大家在Android开发 ...

  5. 自定义控件 闪烁效果的TextView

    使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android ...

  6. 基于Spring MVC的简单HelloWorld实例

    1.导包 2.web.xml文件配置 3.包结构定义以及控制器的编写 4.xxxx-servlet文件配置 5.返回的视图(jsp)编写   6.源码 下载:http://download.csdn. ...

  7. (转)巧用clear:both

    我们在制作网页中用div+css或者称xhtml+css都会遇到一些很诡异的情况,明明布局正确,但是整个画面却混乱起来了,有时候在IE6下看的很正常的,到ie7或者火狐下看时,就一片混乱了,无论怎么计 ...

  8. MVC项目发布IIS访问不了

    首先在配置文件上加红色字体这个配置 用来打印错误信息,再根据错误信息来处理 <system.webServer> <validation validateIntegratedMode ...

  9. 浅谈MDX处理空值NULL及格式化结果

    MDX查询结果中往往会含有"NULL"值,这是某维度下对应的的量值不存在导致的,为了让报表呈现更好的效果,在有些情况下,需要将"NULL"的切片值置换成0,这些 ...

  10. ORA-01172 ORA-01151

    今天发现服务器的9号硬盘坏了,因做了RAID5,报障后也没理会,过了一会儿,有人反应说报表运行不出来,发现服务器所有的硬盘都不工作了,界面也没有反应,就强行关机,再开机可以进入系统,等维修人员来换了新 ...