HDOJ 5396 Expression DP


dp_{l,r}dpl,r表示l,rl,r这段数能形成的答案总和。

枚举最后一步操作kk,如果是乘法,答案为dp_{l,k}*dp_{k+1,r}dpl,kdpk+1,r,由于分配率这个会乘开来。如果是加法那么是dp_{l,r}*(r-k-1)!+dp_{k+1,r}*(k-l)!dpl,r(rk1)!+dpk+1,r(kl)!,即要乘上右边k+1,rk+1,r这些数所有可行的方案数,减法同理。最后乘上{r-l-2 \choose k-l}(klrl2),即把两边操作合起来的方案数。

答案为dp_{1,n}dp1,n


Expression

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


Problem Description
Teacher Mai has  n  numbers  a1,a2,,an and  n1  operators("+", "-" or "*") op1,op2,,opn1 , 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+1i  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  n1  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+4683 " is  1+46831+4(2)31+(8)3(7)321 .
 

Input
There are multiple test cases.

For each test case, the first line contains one number  n(2n100) .

The second line contains  n  integers  a1,a2,,an(0ai109) .

The third line contains a string with length  n1  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
 



/* ***********************************************
Author        :CKboss
Created Time  :2015年08月19日 星期三 21时58分12秒
File Name     :HDOJ5396.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long int LL;

const int maxn=210;
const LL mod=1e9+7LL;

int n;
LL a[maxn];
char ope[maxn],cmd[maxn];
LL dp[maxn][maxn];
LL C[maxn][maxn];
LL jc[maxn];

void init()
{
	jc[0]=1; C[0][0]=1LL;
	for(int i=1;i<maxn;i++) 
	{
		C[i][i]=C[i][0]=1LL;
		jc[i]=(jc[i-1]*i)%mod;
	}
	for(int i=1;i<maxn;i++)
	{
		for(int j=1;j<i;j++) C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
	}
}

void DP()
{
	memset(dp,0,sizeof(dp));
	for(int i=1;i<=n;i++) dp[i][i]=a[i];
	for(int len=2;len<=n;len++)
	{
		for(int i=1;i+len-1<=n;i++)
		{
			int j=i+len-1;
			for(int k=i;k<j;k++)
			{
				int left=k-i;
				int right=len-2-left;
				LL t=0;
				if(ope[k]=='*')
				{
					t=(dp[i][k]*dp[k+1][j])%mod;
				}
				else if(ope[k]=='+')
				{
					t=(dp[i][k]*jc[right]%mod+dp[k+1][j]*jc[left]%mod)%mod;
				}
				else if(ope[k]=='-')
				{
					t=(dp[i][k]*jc[right]%mod-dp[k+1][j]*jc[left]%mod+mod)%mod;
				}
				dp[i][j]=(dp[i][j]+t*C[len-2][left]%mod)%mod;
			}
		}
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	init();
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=1;i<=n;i++) scanf("%lld",a+i);
		scanf("%s",cmd+1);
		for(int i=1;i<n;i++) ope[i]=cmd[i];
		DP();
		printf("%lld\n",dp[1][n]%mod);
	}
    
    return 0;
}



  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值