hdu 5396 Expression 区间DP+排列组合 2015 Multi-University Training Contest 9
来源:互联网 发布:服装生产库存软件 编辑:程序博客网 时间:2024/06/05 18:57
Expression
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 453 Accepted Submission(s): 264
Problem Description
Teacher Mai has n numbers a1,a2,⋯,an and 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. Ini -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 .
He wants to erase numbers one by one. In
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 "
Input
There are multiple test cases.
For each test case, the first line contains one numbern(2≤n≤100) .
The second line containsn integers a1,a2,⋯,an(0≤ai≤109) .
The third line contains a string with lengthn−1 consisting "+","-" and "*", which represents the operator sequence.
For each test case, the first line contains one number
The second line contains
The third line contains a string with length
Output
For each test case print the answer modulo 109+7 .
Sample Input
33 2 1-+51 4 6 8 3+*-*
Sample Output
2999999689HintTwo numbers are considered different when they are in different positions.
Author
xudyh
Source
2015 Multi-University Training Contest 9
Recommend
wange2014 | We have carefully selected several similar problems for you: 5405 5404 5403 5402 5401
区间DP,
dp[i][j]表示区间[i,j]所有可能的计算顺序之后结果的和。
考虑区间i,j最后一个计算的符号位置:设符号左边所有可能的计算顺序之后结果有a1,a2,...,an,右边可能的结果有b1,b2,b3,...,bn。(可重)
计算方法:首先将所有ai 根据最后一个符号和所有bi,+或-或*,然后再对其排列,因为每种ai (+或-或*) bi
左区间和右区间内的符号运行相对顺序可以不同,但是结果相同。
(左右区间自身的顺序是定的,因为用dp之前已算出子区间所有顺序的结果和)
'+':
dp[i][j]=C (组合公式) * ( ∑ai * A( 右区间符号数全排列) + ∑bi *A(左区间符号数全排列) )
(左右区间的相对顺序) ( 对于每个ai,一共加了A( 右区间符号数全排列)多次。)
dp[i][j]=(dp[i][k-1]*A( 右区间符号数全排列)+dp[k][i]*A(左区间符号数全排列))*C( , ); (k=i+1,...,j);
'-':dp[i][j]=(dp[i][k-1]*A( 右区间符号数全排列)-dp[k][i]*A(左区间符号数全排列))*C( , );
'*'::dp[i][j]=(dp[i][k-1]*dp[k][i])*C( , );
d[i][j]=C( ,) *(a1*b1+a1*b2+...+a1*bn+a2*b1+...an*bn)=C( ,) *∑ai*∑bi
#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI 3.1415926535897932384626#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)#define lson num<<1,le,mid#define rson num<<1|1,mid+1,ri#define MID int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)#define mp make_pair#define _f first#define _s secondusing namespace std;const int INF =0x3f3f3f3f;const int maxn= 100+5 ;//const int INF= ;typedef long long ll;const ll mod= 1000000000+7 ;const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);//by yskysker123
int n;int a[maxn];char c[maxn];ll dp[maxn][maxn];ll A[maxn];ll C[maxn][maxn];<span style="font-family: 'Times New Roman';"> //杨辉三角预处理全排列和组合结果</span>void pre(){ A[0]=1; for(ll i=0;i<maxn;i++) //C(0,x),C( x,0)的情况不能漏,这里可以视为1 { if(i>0) A[i]=A[i-1]*i%mod; for(ll j=0;j<=i;j++) { if(j==0||j==i) C[i][j]=1; else { C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod; } } }}int main(){ pre(); while(~scanf("%d",&n)) { FOR0(i,n) scanf("%d",&a[i]); FOR1(i,n-1) scanf(" %c",&c[i]); memset(dp,0,sizeof dp); for(int i=0;i<=n-1;i++) { dp[i][i]=a[i]; } for(int j=0;j<=n-1;j++) //计算的顺序 { for(int i=j-1;i>=0;i--) { ll ret; for(int k=i+1;k<=j;k++) { if(c[k]=='*') { ret= (dp[i][k-1] * dp[k][j])%mod; } else if(c[k]=='+') { ret=(dp[i][k-1]*A[j-k ]%mod +dp[k][j]*A[k-1-i]%mod )%mod; } else { ret=((dp[i][k-1]*A[j-k ]%mod -dp[k][j]*A[k-1-i]%mod )+mod)%mod; } ret=ret*C[j-i-1][k-i-1 ]%mod; dp[i][j]=(dp[i][j]+ret)%mod; } } } printf("%lld\n",dp[0][n-1]); } return 0;}
本题用以下的组合公式不仅超时,而且是错的,因为取了模。
//ll C(ll n,ll m)//{// ll ans=1;// m=min(m,n-m);// for(ll i=1;i<=m;i++)// {// ans=ans*(n-i+1)/i %mod;//// }//// return ans;////}
0 0
- hdu 5396 Expression 区间DP+排列组合 2015 Multi-University Training Contest 9
- 2015 Multi-University Training Contest 9 hdu 5396 Expression
- HDU 5396 Expression ( 2015 Multi-University Training Contest 9)
- 2015 Multi-University Training Contest 9(区间dp)
- hdu 5396 Expression ||2015 Multi-University Training Contest 9 || 简单模拟
- hdu 5791 2016 Multi-University Training Contest 5(dp)
- hdu 6170 dp 2017 Multi-University Training Contest
- 2015 Multi-University Training Contest 9(hdu 5396 - hdu 5405)
- HDU 5396 Expression(区间DP,排列组合)
- hdu 5291 Candy Distribution 2015 Multi-University Training Contest 1 树形dp,
- hdu 5389 Zero Escape DP+数学规律 给出证明 2015 Multi-University Training Contest 8
- hdu 5294 Tricks Device 2015 Multi-University Training Contest 1
- hdu 5289 Assignment 2015 Multi-University Training Contest 1
- 2015 Multi-University Training Contest 1 Hdu 5289 Assignment
- 2015 Multi-University Training Contest 1 Hdu 5292 Pocket Cube
- 2015 Multi-University Training Contest 1 Hdu 5297 Y sequence
- HDU 5289 Assignment (2015 Multi-University Training Contest 1)
- hdu 5291 Candy Distribution 2015 Multi-University Training Contest 1
- Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现
- Deep Learning论文笔记之(五)CNN卷积神经网络代码理解
- 《Java设计模式》之状态模式
- 过度疲劳易诱发癌症!从疲劳到癌症只有5步!我们要把危险扼杀在摇篮!
- The Perfect Stall(POJ_1274)
- hdu 5396 Expression 区间DP+排列组合 2015 Multi-University Training Contest 9
- 数据持久化
- 《Java设计模式》之策略模式
- Android学习之ScollView嵌套ListView和GridView问题
- asp.net CheckBox 綁定Boolean 類型
- scikit-learn:class and function reference(看看你到底掌握了多少。。)
- android studio的安装
- iOS的电话号码344格式以及判断电话号码的正则表达式
- 多线程并发问题(三)