Expression

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

Problem Description
Teacher Mai has n numbers $a_1,a_2,\dots,a_n$ and $n-1$ operators ("+","-" or "*")$op_1,op_2,\dots,op_{n-1}$ which are arranged in the form $a_1\quad op_1 \quad a_2\quad op_2\quad a_3\dots\quad a_n$

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\leq n \leq 100)$.

The second line contains n integers $a_1,a_2,⋯,a_n\quad(0≤a_i \leq 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.

 
Author
xudyh
 
Source
 
解题:区间动规,尼玛,最坑爹的地方在于区间合并的时候容易把合并也是有时序的忘记乘入了。
 
c[t-j-1][k-j]就是干这个用的 假设左边的顺序固定,右边的顺序固定,合并后,还得保持来自左边的那些操作符的相对操作顺序,右边的也一样,所以是组合数
 
关于阶乘,其实这么多个操作符,有多少种操作顺序?当然是阶乘个
 
至于+-法的合并,可以发现假设A代表里面有3个和的和,B代表里面有2个和的和
 
A(a,b,c) + B(d+e)
 
由于我们要求每种可能
 
那么将产生 
 
$a + d \quad b + d \quad c + d$
$a + e \quad b + e \quad c + e$
 
可以发现其等价于 2A + 3B A里面的元素都加了两次,B里面的元素都加了3次
 
好啦dp吧,没想到要用到$\binom{0}{0}$害我debug好久
 
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
const LL mod = ;
LL dp[maxn][maxn],c[maxn][maxn] = {},f[maxn] = {};
void init() {
for(int i = ; i < maxn; ++i) {
c[i][] = c[i][i] = ;
f[i] = f[i-]*i%mod;
for(int j = ; j < i; ++j)
c[i][j] = (c[i-][j-] + c[i-][j])%mod;
}
}
char op[maxn];
int main() {
init();
int n;
while(~scanf("%d",&n)) {
memset(dp,,sizeof dp);
for(int i = ; i < n; ++i)
scanf("%I64d",&dp[i][i]);
scanf("%s",op);
for(int i = ; i <= n; ++i) {
for(int j = ; j + i <= n; ++j) {
for(int k = j,t = j + i -; k < t; ++k) {
LL tmp;
if(op[k] == '+')
tmp = (f[t-k-]*dp[j][k] + f[k-j]*dp[k+][t])%mod;
else if(op[k] == '-') {
tmp = (f[t-k-]*dp[j][k] - f[k-j]*dp[k+][t])%mod;
tmp = (tmp + mod)%mod;
} else if(op[k] == '*') tmp = dp[j][k]*dp[k+][t]%mod;
tmp = tmp*c[t-j-][k-j]%mod;
dp[j][t] = (dp[j][t] + tmp + mod)%mod;
}
}
}
printf("%I64d\n",dp[][n-]);
}
return ;
}
 

2015 Multi-University Training Contest 9 hdu 5396 Expression的更多相关文章

  1. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  2. 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!

    Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...

  3. 2015 Multi-University Training Contest 8 hdu 5385 The path

    The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...

  4. 2015 Multi-University Training Contest 3 hdu 5324 Boring Class

    Boring Class Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. 2015 Multi-University Training Contest 3 hdu 5317 RGCDQ

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  6. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  7. 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  8. 2015 Multi-University Training Contest 6 hdu 5362 Just A String

    Just A String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. 2015 Multi-University Training Contest 6 hdu 5357 Easy Sequence

    Easy Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

随机推荐

  1. Bootstrap的js插件之警告框(alert.js)

    data-dismiss="alert"--为关闭button加入该属性能够使其自己主动为警告框赋予关闭功能. .fade .in--为警告框在关闭时加入动画效果. 很多其它细节參 ...

  2. hdu2430 Beans 单调队列

    // hdu2430 Beans 单调队列 // // 题目意思: // 求一个sum%p<=k的max(sum/p) // // 结题报告: // 技巧,先求出前缀和,并记录前i项对p取余的值 ...

  3. jcaptcha进阶

    1.改动CaptchaServiceSingleton类.使用带參构造方法来创建DefaultManageableImageCaptchaService对象. watermark/2/text/aHR ...

  4. Dark roads--hdoj

    Dark roads Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Su ...

  5. java的重载总结

    1.不能以返回值的不同来重载方法,编译都不通过(只有参数类型或者参数个数不同才可以重载方法) 在Java语言中,要重载一个方法,除了要与原方法具有相同的简单名称外,还要求必须拥有一个与原方法不同的(不 ...

  6. 创建一个netcore2.0和angular的项目并运行起来

    netcore2.0发布了,喜大普奔. 我们先下载SDK,请看张善友老师的这篇博客 http://www.cnblogs.com/shanyou/p/7363037.html 下载完之后 我用的vs2 ...

  7. Codeforces Round #445

    ACM ICPC 每个队伍必须是3个人 #include<stdio.h> #include<string.h> #include<stdlib.h> #inclu ...

  8. Android stroke 边框线 某一边

    有时候需要给View加边框线,我们经常是四边一起加,就像这样: <shape xmlns:android="http://schemas.android.com/apk/res/and ...

  9. RAP开发入门-开发笔记-bug记录

    NamespaceException: The alias '/rwt-resources' is already in use 该bug发生的第一种情况是: This means that more ...

  10. Junit使用第二弹

    实例总结 1. 参数化测试 有时一个测试方法,不同的参数值会产生不同的结果,那么我们为了测试全面,会把多个参数值都写出来并一一断言测试,这样有时难免费时费力,这是我们便可以采用参数化测试来解决这个问题 ...