您的位置:首页 > 大数据 > 人工智能

2015 Multi-University Training Contest 9 hdu 5396 Expression

2015-08-18 20:55 573 查看

Expression

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


[align=left]Problem Description[/align]
[align=left]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$[/align]

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

[align=left]Sample Input[/align]

3
3 2 1
-+
5
1 4 6 8 3
+*-*

[align=left]Sample Output[/align]

2

999999689

Hint

Two numbers are considered different when they are in different positions.

[align=left]Author[/align]
xudyh

[align=left]Source[/align]
2015 Multi-University Training Contest 9 1001

解题:区间动规,尼玛,最坑爹的地方在于区间合并的时候容易把合并也是有时序的忘记乘入了。

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 = 110;
const LL mod = 1000000007;
LL dp[maxn][maxn],c[maxn][maxn] = {1},f[maxn] = {1};
void init() {
for(int i = 1; i < maxn; ++i) {
c[i][0] = c[i][i] = 1;
f[i] = f[i-1]*i%mod;
for(int j = 1; j < i; ++j)
c[i][j] = (c[i-1][j-1] + c[i-1][j])%mod;
}
}
char op[maxn];
int main() {
init();
int n;
while(~scanf("%d",&n)) {
memset(dp,0,sizeof dp);
for(int i = 0; i < n; ++i)
scanf("%I64d",&dp[i][i]);
scanf("%s",op);
for(int i = 2; i <= n; ++i) {
for(int j = 0; j + i <= n; ++j) {
for(int k = j,t = j + i -1; k < t; ++k) {
LL tmp;
if(op[k] == '+')
tmp = (f[t-k-1]*dp[j][k] + f[k-j]*dp[k+1][t])%mod;
else if(op[k] == '-') {
tmp = (f[t-k-1]*dp[j][k] - f[k-j]*dp[k+1][t])%mod;
tmp = (tmp + mod)%mod;
} else if(op[k] == '*') tmp = dp[j][k]*dp[k+1][t]%mod;
tmp = tmp*c[t-j-1][k-j]%mod;
dp[j][t] = (dp[j][t] + tmp + mod)%mod;
}
}
}
printf("%I64d\n",dp[0][n-1]);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: