Expression

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 952    Accepted Submission(s): 573

Problem Description
Teacher Mai has n

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

.

 
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≤10^9)

.

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 10^9+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.

一看这样子就像是区间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的更多相关文章

  1. ACM学习历程—HDU5396 Expression(递推 && 计数)

    Problem Description Teacher Mai has n numbers a1,a2,⋯,an and n−1 operators("+", "-&qu ...

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

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

  3. [hdu5396 Expression]区间DP

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

  4. 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 ...

  5. OpenCASCADE Expression Interpreter by Flex & Bison

    OpenCASCADE Expression Interpreter by Flex & Bison eryar@163.com Abstract. OpenCASCADE provide d ...

  6. Expression Blend创建自定义按钮

    在 Expression Blend 中,我们可以在美工板上绘制形状.路径和控件,然后修改其外观和行为,从而直观地设计应用程序.Button按钮也是Expression Blend最常用的控件之一,在 ...

  7. [C#] C# 知识回顾 - 表达式树 Expression Trees

    C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...

  8. Could not evaluate expression

    VS15 调试变量不能显示值,提示:Could not evaluate expression 解决办法: 选择"在调试时显示运行以单击编辑器中的按钮"重启VS即可. 可参考:Vi ...

  9. 使用Expression实现数据的任意字段过滤(1)

    在项目常常要和数据表格打交道. 现在BS的通常做法都是前端用一个js的Grid控件, 然后通过ajax的方式从后台加载数据, 然后将数据和Grid绑定. 数据往往不是一页可以显示完的, 所以要加分页: ...

随机推荐

  1. 理解Vue

    Vue.js是JavaScript MVVM(Model-View-ViewModel)库,十分简洁,Vue核心只关注视图层,相对AngularJS提供更加简洁.易于理解的API.Vue尽可能通过简单 ...

  2. gendiff - 致力于创建无错的 diff 文件的工具

    SYNOPSIS gendiff <directory> <diff-extension> DESCRIPTION gendiff 是一个简单的脚本,目标是根据单一的目录生成一 ...

  3. false - (失败的)什么都不做

    总览 (SYNOPSIS) false [忽略命令行参数] false OPTION 描述 (DESCRIPTION) 程序 结束 时, 产生 表示 失败 的 状态码. 下列的 选项 没有 简写 形式 ...

  4. NULL Pointer Dereference(转)

    0x00 漏洞代码 null_dereference.c: #include <linux/init.h> #include <linux/module.h> #include ...

  5. jpeg和jpg的区别是什么

    JPG是JPEG的简写,jpg是后缀名,jpeg既可作为后缀名,又能代表文件格式:JPG——JPEG文件格式. 我们在系统自带的画图程序里保存文件,在保存类型:JPEG(*.JPG,*.JPEG,*. ...

  6. (35)zabbix Event acknowledgment事件确认

    概述 以往服务器出现报警,运维人员处理完事之后,报警自动取消,但是下一次出现同样一个错误,但是换了一个运维人员,他可能需要重新排查问题,直到问题处理完毕. 针对这种情况,zabbix提供了event ...

  7. scrollTop如何实现click后页面过渡滚动到顶部

    用JS操作,body元素的scrollTop var getTop = document.getElementById("get-top"); var head = documen ...

  8. 图像分割loss集合

    我们只是大佬的搬运工 1.log loss 2.WBE loss 带权重的交叉熵 3.Focal loss 容易过拟合?我在VGG16上做过实验(没有BN层),发现网络在训练集上的性能直线上升,但是验 ...

  9. cs229_part4

    又到了一节很重要的课,因为这个学习理论是从统计角度为机器学习算法提供了一个理论基础. 学习理论 问题背景 先回顾一下我们第一节课提到的机器学习的组成: 第一节课只是简单的提了一下,现在我们要真正来分析 ...

  10. Android AAR 混淆的坑

    一定不要忘记加上这段 -keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,A ...