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绑定. 数据往往不是一页可以显示完的, 所以要加分页: ...
随机推荐
- js3
举几个小例子: 1. 九九乘法表 var s = "<table>"; for (var i=1;i<=9;i++) { s += "<tr> ...
- 洛谷 P2947 [USACO09MAR]仰望Look Up
题目描述 Farmer John's N (1 <= N <= 100,000) cows, conveniently numbered 1..N, are once again stan ...
- 如何删除github上项目的文件
1. 你要有前面一章的开发平台和github插件,下面就是基于前面来做的. 如何删掉你github上的文件呢?想必你的电脑有一个下载的git工具了,如果还是没有的话,请用npm下载一个git.这是我已 ...
- Lesson2
#ifdef __cplusplus #include <cstdlib> #else #include <stdlib.h> #endif #include <SDL/ ...
- struts2默认拦截器defaultStack
第一次使用Struts2.3.20,前台页面向后台传值,怎么总是提示值为null,查找半天原来是struts配置文件的action中忘记引入defaultStack拦截器了,所以即使Action中万事 ...
- poj2104 K大数 划分树
题意:给定一个数列,求一个区间的第K大数 模板题, 其中的newl, newr 有点不明白. #include <iostream> #include <algorithm> ...
- 多线程threadvar 变量设定
Delphi管理多线程之线程局部存储:threadvar 尽管多线程能够解决许多问题,但是同时它又给我们带来了很多的问题.其中主要的问题就是:对全局变量或句柄这样的全局资源如何访问?另外,当必须确保一 ...
- (4)JSTL的SQL标签库
jstl的SQL标签库 SQL tag Library中的标签用来提供在 JSP 页面中可以与数据库进行交互的功能Database access标签库有以下6组标签来进行工作: <sql:set ...
- Promise中的next 另一个用法
const chainAsync = fns => { let curr = 0 ; const next = (...args) => fns[curr++](next,...args) ...
- Day09文件操作
1.什么是文件 文件:操作系统为应用程序或者用户提供的一种操作硬盘的虚拟单位 强调:①文件是操作系统提供的虚拟单位②应用程序或者用户对文件的读写操作其实都是向操作系统发送指令 2.为什么要用文件 文件 ...