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. 微软爆料新型系统,Windows7,Windows10强势来袭

    本系统是10月5日最新完整版本的Windows10 安装版镜像,win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为win10 Edge浏览器中国默认主页和搜索引擎 ...

  2. 洛谷 P1438 无聊的数列

    题目背景 无聊的YYB总喜欢搞出一些正常人无法搞出的东西.有一天,无聊的YYB想出了一道无聊的题:无聊的数列...(K峰:这题不是傻X题吗) 题目描述 维护一个数列{a[i]},支持两种操作: 1.1 ...

  3. CAD交互绘制矩形批注(网页版)

    js中实现代码说明: 动态拖放时的绘制事件: function DynWorldDrawComment2( pCustomEntity,pWorldDraw, curPt) { // 得到绘制参数. ...

  4. 剑指offer42 左旋转字符串

    自己想的一个新的写法,如果不排除length=0的情况,下面那个while是死循环 class Solution { public: string LeftRotateString(string st ...

  5. CPP-基础:关于内存分配

    1:c中的malloc和c++中的new有什么区别 (1)new.delete 是操作符,可以重载,只能在C++中使用.(2)malloc.free是函数,可以覆盖,C.C++中都可以使用.(3)ne ...

  6. Codeforces 727C Guess the Array

    题目传送门 长度为\(n\)的数组,询问\(n\)次来求出数组中每一个数的值 我们可以先询问三次 \(a[1]+a[2]\) \(a[1]+a[3]\) \(a[2]+a[3]\) 然后根据这三次询问 ...

  7. 人脸识别中的检测(在Opencv中加入了QT)

    #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include & ...

  8. Spring框架针对dao层的jdbcTemplate操作crud之delete删除数据库操作 Spring相关Jar包下载

    首先,找齐Spring框架中IoC功能.aop功能.JdbcTemplate功能所需的jar包,当前13个Jar包 1.Spring压缩包中的四个核心JAR包,实现IoC控制反转的根据xml配置文件或 ...

  9. Swift在1.2版本的变化

    从Xcode 6.3 Beta Release Notes看出,Xcode 6.3 Beta包含了很多颇为值得开发者期待的改变,共计50多处改动,同时修改了Objective-C的语法,足见苹果对Sw ...

  10. javascript变量名命名规则

    1. js变量名可以包含数字,字母,$及_,不能以数字开头. 2. js变量可以使用中文,但是最好不要这么命名,以避免不必要的麻烦.