hdu 5396 Expression(区间dp)
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.
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.
3 2 1
-+
5
1 4 6 8 3
+*-*
999999689
Two numbers are considered different when they are in different positions.
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)的更多相关文章
- HDU 5396 Expression(DP+组合数)(详解)
题目大意: 给你一个n然后是n个数. 然后是n-1个操作符,操作符是插入在两个数字之间的. 由于你不同的运算顺序,会产生不同的结果. 比如: 1 + 1 * 2 有两种 (1+1)*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 ...
- Dire Wolf HDU - 5115(区间dp)
Dire Wolf Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)Total ...
- [hdu5396 Expression]区间DP
题意:给一个表达式,求所有的计算顺序产生的结果总和 思路:比较明显的区间dp,令dp[l][r]为闭区间[l,r]的所有可能的结果和,考虑最后一个符号的位置k,k必须在l,r之间,则l≤k<r, ...
- 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 ...
- HDU 5568 sequence2 区间dp+大数
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5568 题意: 求所有长度为k的严格升序子序列的个数. 题解: 令dp[i][k]表示以i结尾的长度为 ...
- hdu 4579 博弈+区间dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 #include <cstdio> #include <cstring> ...
- hdu 5396 Expression
考虑到此题麻烦了某hust大神&体现出了自己数学能力的欠缺 虽然最近一直比较忙 还是把这题的题解写下来吧 首先看完数据范围后 应该有不少人会反应到是$n^3$的DP 以$F[i][j]$表示从 ...
- hdu5396 Expression 区间dp +排列组合
#include<stdio.h> #include<string> #include<map> #include<vector> #include&l ...
随机推荐
- lesson6:java线程中断
正常的情况下,业务系统都不会去中断它的线程,但是由于一些特殊情况的发生,线程已经不能正常结束了,并且此类线程已经影响到业务系统提供服务的能力,如果系统设计的健壮,便会通过监控线程去主动的中断此类线程. ...
- spring技术翻译开始
从今天开始,我会坚持每天花费两个小时来翻译一本英文书(当然自己觉得绝对算得上是经典),可能我英文水平有限,但也请路过的高人予以指点. 如果有翻译的出入很大,望各位告知,本人一定更改.决定翻译的目的有两 ...
- swift 中String常用操作
1. 字符串定义 var s = "aaaaaa" // 两个字符串均为空并等价. var emptyString = "" var anotherEmp ...
- 模块计算机类型“X64”与目标计算机类型“x86”冲突
问题描述:在X64 平台上开发dll 文件,在生成dll时Vs 2010 出现如下错误 :"fatal error LNK1112: 模块计算机类型"X64"与目标计算机 ...
- Python-字符串开头或结尾匹配
startswith() 和 endswith() 方法提供了一个非常方便的方式去做字符串开头和结尾的检查. 1.查看指定目录下的所有文件名 >>> import os >&g ...
- window成员和document成员
输出浏览器成员和DOM成员(以下为safari浏览器测试)(浏览器不同对象成员有差异) window成员 <script type="text/javascript"> ...
- mvc自带的异步表单提交
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 关于ajax网络请求的封装
// 封装的ajax网络请求函数// obj 是一个对象function AJAX(obj){ //跨域请求 if (obj.dataType == "jsonp") ...
- 一小时搞定DIV+CSS布局-固定页面开度布局
本文讲解使用DIV+CSS布局最基本的内容,读完本文你讲会使用DIV+CSS进行简单的页面布局. 转载请标明:http://www.kwstu.com/ArticleView/divcss_20139 ...
- Dom4j之xPath
XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointe ...