2015 Multi-University Training Contest 9 hdu 5396 Expression
Expression
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 175 Accepted Submission(s): 95
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$.
Two numbers are considered different when they are in different positions.
#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的更多相关文章
- 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 ...
- 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: ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 判断是否是pc,获取屏幕宽度
$(function(){ var w=document.documentElement?document.documentElement.clientWidth:document.body.clie ...
- bzoj4465: [Jsoi2013]游戏中的学问
DP 一个人要么加入一个圈,要么三个人开一圈 #include<cstdio> #include<iostream> #include<cstring> #incl ...
- PHP 和 Java 的主要区别有哪些?
PHP 和 Java 的主要区别有哪些? 其实Java方面我要学的真的还有很多,要是有大项目的机会和经验就好,所以提前我肯定要把基础打扎实. 我要学的还有很多,比如前段,后端,还有linux,还有肯定 ...
- nyoj--744--蚂蚁的难题(一)
蚂蚁的难题(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 小蚂蚁童鞋最近迷上了位运算,他感觉位运算非常神奇.不过他最近遇到了一个难题: 给定一个区间[a,b],在 ...
- ServletContextAware、ServletRequestAware、ServletResponseAware、SessionAware
转自:ServletContextAware.ServletRequestAware.ServletResponseAware.SessionAware Struts 2提供了Aware接口.Awar ...
- 2-1 Restful中HTTP协议介绍
Restful是一种基于资源的软件架构风格,所以从定义上来说是跟HTTP无关的.但是本课程提到的Restful API是基于HTTP协议的一种实现.所有相关知识都是基于现有的HTTP协议而来,并没有对 ...
- 【正则表达式】从json数组中抽取id列表
有如下数组,要从中取出id: "[\"3812662409\",\"3812633637\",\"3812627686\",\&q ...
- Linq怎么支持Monad
在上一篇创建了我们的第一个Monad, Identity<T>. 我们确定了类型要变成Monad, 它必须有一个type constructor(Identity<T>), 和 ...
- P1732 活蹦乱跳的香穗子
题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现自己很无聊,于是她想了一个有趣的事情,每个格子最多只能经过1次,且每个格子都有其价值 跳的规则是这样的,香穗子可以向上下左右四个方向跳到相邻的格子,并且她 ...
- jquery 获取及设置input各种类型的值
获取选中的值 获取一组radio被选中项的值 var item = $(“input[@name=items]:checked”).val(); 获取select被选中项的文本 var item = ...