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. Xcode4删除文件后missing file警告

    1.运行终端,执行命令行进入missing file目录,然后运行 svn delete nameOfMissingFile 或 git rm nameOfMissingFile 2.删除隐藏的.sv ...

  2. BCB:AnsiString和String的区别

    AnsiString和String的区别.使用 本文转自:http://www.bianceng.cn/c/index.htm 16.C/C++语言在CB中的一些特定用法 2)AnsiString是从 ...

  3. sstable, bigtable,leveldb,cassandra,hbase的lsm基础

    先看懂文献1和2 1. 先了解sstable.SSTable: Sorted String Table [2] [10] WiscKey:  类似myisam, key value分离, 根据ssd优 ...

  4. Ukulele 常用和弦

  5. C语言运算符_03

    ·运算符的优先级:C语言中,运算符的优先级共分为15级.1级最高,15级最低.在表达式中,优先级较高的先于优先级较低的进行运算.而在同一个运算量两侧的运算符优先级相同时,则按运算符的结合性所规定的结合 ...

  6. 对于WebAssembly编译出来的.wasm文件js如何调用

    WebAssembly也叫浏览器字节码技术 这里就不过多的解释了网上很多介绍 主要是让大家知道在js里面如何调用执行它,我之前看WebAssemblyAPI时候反正是看得一脸懵逼 也是为了大家能更快的 ...

  7. windows 使用git上传代码至github

    1. 首先创建github账户 2. 创建github项目 3.  windows安装git工具 ·下载地址:https://git-for-windows.github.io/ ,下载直接安装即可, ...

  8. ssh 免密码登录 与 密钥公钥原理讲解

    前言 由于最近频繁需要登录几个服务器,每次登录都需要输入密码,故相对麻烦. 由于个人服务器用于实验,故对安全性要求不是很高,故想实现ssh免密登录. 通过阅读ssh 公钥私钥认证操作及原理以及ssh公 ...

  9. 搭建pip源

    1.安装pip软件 yum -y install python-pippip install --upgrade pippip install pip2pi 2.安装apacheyum -y inst ...

  10. PWA介绍

    https://codelabs.developers.google.com/codelabs/your-first-pwapp/#0 PWA是一些技术的集合.用于消除web与其他客户端之间的差距,最 ...