hdu5396Expression(区间dp,好题)
2015-09-16 16:11
274 查看
Expression
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/OthersProblem Description
Teacher Mai has n
numbers a1,a2,⋯,anand
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.
Input
There are multiple test cases.
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≤109).
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
109+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
2015 Multi-University Training Contest 9
首先,如果将要进行的符号运算是*,设左边运算出的答案分别为a1,a2,a3...an,右边的答案为b1,b2,b3,b4...bn;
则左边乘以右边为a1*(右边的和)+a2*(右边的和)+...,也为dp[l][k]*dp[k+1][r];
如果将要进行的符号运算是+,设左边有n=(k-l)!组答案,右边为有m=(r-k)组答案,
设左边每组答案分别为a1,a2,a3...,右边每组答案为b1,b2,b3....
所以答案为a1+b1+a1+b2+...a1+bn=(右边的情况数*ai-右边的和)*左边的情况数=dpl,r∗(r−k−1)!+dpk+1,r∗(k−l)!
最后要确定左边操作和右边操作的顺序 因为每个答案里是统计了该区间所有的阶乘情况,因此每一个左边已确定的顺序和右边已确定的顺序需要排列组合一下。比如:左边有3个操作数+-*,右边有2个操作符+-,当已经确定了他们各自的顺序,假设左边算-*+,右边+-,这个顺序已经固定,现在有五个操作符需要操作,我需要选择三个位置给左边的操作符-*+,那么右边的两个操作符自然就对应他们相应的位置。
#include<bits/stdc++.h> #include<iostream> using namespace std; typedef long long ll; const int MOD=1e9+7; char s[111]; ll dp[111][111]; int num[111]; ll A[111]; ll C[111][111]; void solve(int n){ for(int k=2;k<=n;k++) //长度 for(int i=1;i+k-1<=n;i++) //从哪里开始 for(int j=i;j-i+1<k;j++){ //到哪里结束 if(s[j]=='-'){ dp[i][i+k-1]=(dp[i][i+k-1]+(A[i+k-j-2]*dp[i][j]%MOD-A[j-i]*dp[j+1][i+k-1]%MOD)*C[k-2][j-i]%MOD)%MOD; //cout<<"i is "<<i<<" j is "<<j<<" k is "<<k<<" dp["<<i<<"]["<<i+k-1<<"] is "<<dp[i][i+k-1]<<endl; } else if(s[j]=='+'){ dp[i][i+k-1]=(dp[i][i+k-1]+(A[j-i]*dp[j+1][i+k-1]%MOD+A[i+k-j-2]*dp[i][j]%MOD)*C[k-2][j-i]%MOD)%MOD; //cout<<"i is "<<i<<" j is "<<j<<" k is "<<k<<" dp["<<i<<"]["<<i+k-1<<"] is "<<dp[i][i+k-1]<<endl; } else if(s[j]=='*'){ dp[i][i+k-1]=(dp[i][i+k-1]+dp[j+1][i+k-1]*dp[i][j]%MOD*C[k-2][j-i]%MOD)%MOD; //cout<<"i is "<<i<<" j is "<<j<<" k is "<<k<<" dp["<<i<<"]["<<i+k-1<<"] is "<<dp[i][i+k-1]<<endl; } } } void init(){ A[0]=1; A[1]=1; for(int i=2;i<=101;i++) A[i]=A[i-1]*i%MOD; for(int i=0;i<101;i++){ C[i][0]=C[i][i]=1; for(int j=1;j<=i;j++) C[i][j]=(C[i-1][j-1]+C[i-1][j])%MOD; } } int main(){ int n; init(); while(scanf("%d",&n)!=EOF){ memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++){ scanf("%d",&num[i]); dp[i][i]=num[i]; } scanf("%s",s+1); solve(n); printf("%I64d\n",(dp[1] %MOD+MOD)%MOD); } return 0; }
相关文章推荐
- Android开发中那些你不知道的事
- 大公司里怎样开发和部署前端代码?
- Workflow Learing
- git_cmd和gihub使用说明
- 软件版本说明
- MyBatis学习总结(七)——Mybatis缓存
- 简单的表单验证
- JQUERY实现拖拽进度条显示百分比
- mysql导入导出备份与恢复
- exit()与_exit()函数的区别(Linux系统中)
- GCC编译器(1)
- c++ string总结
- 动态代理模式(设计模式10)
- Magento EAV模型
- poj2082栈的简单应用
- ASP.NET网站页面加载及运行效率等多方面实战优化
- web_apache与tomcat
- MyBatis学习总结(六)——调用存储过程
- 只用串口和网络裸机开发ARM程序(OK6410开发板)纯(转)
- Tapestry5.3.8 Web Services开发