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 ...
随机推荐
- 【java项目实战】ThreadLocal封装Connection,实现同一线程共享资源
线程安全一直是程序员们关注的焦点.多线程也一直是比較让人头疼的话题,想必大家以前也遇到过各种各种的问题.我就不再累述了.当然,解决方案也有非常多,这篇博文给大家提供一种非常好的解决线程安全问题的思路. ...
- Linux Shell Scripting Cookbook 读书笔记 1
本系列文章为<Linux Shell Scripting Cookbook>的读书笔记,只记录了我觉得工作中有用,而我还不是很熟练的命令 书是很好的书,有许多命令由于我比较熟悉,可能就没有 ...
- Laravel-查询作用域
Laravel-查询作用域 标签(空格分隔): php, laravel 全局作用域 ## 编写全局作用域 ## 编写全局作用域很简单.定义一个实现 Illuminate\Database\Eloqu ...
- Redis List 命令技巧
1.实现栈的功能(先进后出) lpush + lpop = stack > lpush mylist (integer) > lpop mylist " > lpop my ...
- C - Game With Sticks
Problem description After winning gold and silver in IOI 2014, Akshat and Malvika want to have some ...
- A - Boy or Girl(set)
Problem description Those days, many boys use beautiful girls' photos as avatars in forums. So it is ...
- NFS 开机自动挂载共享目录
开机自动挂载: 如果服务端或客户端的服务器重启之后需要手动挂载,我们可以加入到开机自动挂载 在服务端/客户端的/etc/fstab里添加 192.168.22.204:/opt/filestore ...
- 第一个Hibernate程序
一 新建一个Java工程(Hibernate) 在src目录下创建一个名为"hibernate.cfg.xml"的文件并配置好各个属性,如下: <?xml version=& ...
- App Store兼容性问题
app下载出现兼容性问题 项目支持9.0以上的系统 但是10.3的iphone5下载的一直是老版本app 下载时提示不兼容 导致无法正常使用 解决办法: 修改Build-Settings-> ...
- 【Oracle】手工建库启动到nomount状态时错误ORA-09925,ORA-01017
配置好pfile和口令文件后启动数据库到nomount状态下出现错误: [oracle@localhost ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2 ...