hdu5396 Expression
Expression
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 952 Accepted Submission(s): 573
numbers a1,a2,⋯,an
and 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
.
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≤10^9)
.
The third line contains a string with length n−1
consisting "+","-" and "*", which represents the operator sequence.
.
Two numbers are considered different when they are in different positions.
一看这样子就像是区间dp
再看看数据肯定是区间dp
f[i][j]表示区间[i,j]一共(j-i)!种运算得到的所有数之和
加减都很简单,枚举区间[i,j]中i到j-1中间最后一个运算符k
如果是加号,f[i][j]+=(f[i][k]*(j-k-1)!+f[k+1][j]*(k-i)!)*C(j-i-1,k-i)
意思就是考虑左右两边的f[i][k],f[k+1][j]对f[i][j]的影响
如果是减号,把上面+改-
乘法不会,orz了某神犇之后才知道
f[i][j]+=f[i][k]*f[k+1][j]*C(j-i-1,k-i)
#include<cstdio>
#include<cstring>
#define LL long long
#define mod 1000000007
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int c[][];
LL a[];
char s[];
LL f[][];
LL jc[];
inline void init()
{
c[][]=;
for (int i=;i<=;i++)c[i][]=;
for (int i=;i<=;i++)
for (int j=;j<=i;j++)
c[i][j]=(c[i-][j]+c[i-][j-])%mod;
jc[]=;
for (int i=;i<=;i++)jc[i]=jc[i-]*i%mod;
}
int n;
int main()
{
init();
while (~scanf("%d",&n))
{
memset(f,,sizeof(f));
for (int i=;i<=n;i++)a[i]=read();
scanf("%s",s+);
for (int i=;i<=n;i++)f[i][i]=a[i];
for (int len=;len<=n;len++)
for (int i=;i<=n;i++)
{
int j=i+len-;if (j>n)break;
for (int k=i;k<j;k++)
{
if (s[k]=='+')f[i][j]=(f[i][j]+(f[i][k]*jc[j-k-]+f[k+][j]*jc[k-i])%mod*c[j-i-][k-i]%mod)%mod;
if (s[k]=='-')f[i][j]=(f[i][j]+(f[i][k]*jc[j-k-]-f[k+][j]*jc[k-i])%mod*c[j-i-][k-i]%mod+mod)%mod;
if (s[k]=='*')f[i][j]=(f[i][j]+(f[i][k]*f[k+][j])%mod*c[j-i-][k-i])%mod;
}
}
printf("%lld\n",f[][n]);
}
}
hdu 5396
hdu5396 Expression的更多相关文章
- ACM学习历程—HDU5396 Expression(递推 && 计数)
Problem Description Teacher Mai has n numbers a1,a2,⋯,an and n−1 operators("+", "-&qu ...
- hdu5396 Expression 区间dp +排列组合
#include<stdio.h> #include<string> #include<map> #include<vector> #include&l ...
- [hdu5396 Expression]区间DP
题意:给一个表达式,求所有的计算顺序产生的结果总和 思路:比较明显的区间dp,令dp[l][r]为闭区间[l,r]的所有可能的结果和,考虑最后一个符号的位置k,k必须在l,r之间,则l≤k<r, ...
- AutoMapper:Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 应用场景:ViewModel==>Mode映射的时候出错 AutoMappe ...
- OpenCASCADE Expression Interpreter by Flex & Bison
OpenCASCADE Expression Interpreter by Flex & Bison eryar@163.com Abstract. OpenCASCADE provide d ...
- Expression Blend创建自定义按钮
在 Expression Blend 中,我们可以在美工板上绘制形状.路径和控件,然后修改其外观和行为,从而直观地设计应用程序.Button按钮也是Expression Blend最常用的控件之一,在 ...
- [C#] C# 知识回顾 - 表达式树 Expression Trees
C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...
- Could not evaluate expression
VS15 调试变量不能显示值,提示:Could not evaluate expression 解决办法: 选择"在调试时显示运行以单击编辑器中的按钮"重启VS即可. 可参考:Vi ...
- 使用Expression实现数据的任意字段过滤(1)
在项目常常要和数据表格打交道. 现在BS的通常做法都是前端用一个js的Grid控件, 然后通过ajax的方式从后台加载数据, 然后将数据和Grid绑定. 数据往往不是一页可以显示完的, 所以要加分页: ...
随机推荐
- SQLServer查询死锁
--查询死锁 select request_session_id spid, OBJECT_NAME(resource_associated_entity_id) tableName from sys ...
- 批处理文件 bat
删除D盘的所有文件:del /a /f /q d:\*.* 删除指定目录的指定扩展名的文件:del /a /f /q 目录:\*.jpg 删除当前目录下的指定扩展名的文件(指定扩展名为jpg):del ...
- 简单明了理解Java移位运算符
无须多言: @Test public void intro() { assertThat("应该相等", -1 >> 1, equalTo(-1)); assertTh ...
- CDN加速静态文件服务器的访问
1.用于加速用户下载资源的速度. 简单来说,CDN相当于一个中间代理,原来我们需要请求某个网址比如www.baidu.com,请求会直接发送至百度的服务器上,假如请求者在新疆,但百度的服务器在北京,这 ...
- 2006: C语言实验——拍皮球
2006: C语言实验——拍皮球 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 231 Solved: 162[Submit][Status][Web ...
- swift-通知的基本使用
swift-通知的基本使用 //通知的使用 1.发通知.(以这条通知为例,通知名字:nickNameNotification 通知参数:title) NSNotificationCenter.de ...
- css flew 布局 解决父元素高度不固定,子级居中。
给父级添加 display: flex; justify-content: flex-start; align-items: center; 子级里的内容永远居中
- (17)zabbix自定义用户key与参数User parameters
为什么要自定义KEY 有时候我们想让被监控端执行一个zabbix没有预定义的检测,zabbix的用户自定义参数功能提供了这个方法. 我们可以在客户端配置文件zabbix_angentd.conf里面配 ...
- nginx 无法加载css/js图片等文件 404 not fund
刚配置Nginx反向代理,Nginx可能会出现无法加载css.js或者图片等文件,这里需要在配置文件*.conf里面加上如下配置项. location ~ .*\.(js|css|png|jpg)$ ...
- rom bist scripts
rom bist 的input 有rom_content file .校验rom还坏,主要通过signature比较.signature跟rom content file 一一对应的. rom bis ...