首页 > 其他 > 详细

HDOJ 5396 Expression DP

时间:2015-08-20 01:29:36      阅读:223      评论:0      收藏:0      [点我收藏+]


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

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

答案为dp_{1,n}dp?1,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,?,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?31+4?(?2)?31+(?8)?3(?7)?3?21.
 

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 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
 



/* ***********************************************
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;
}



版权声明:来自: 码代码的猿猿的AC之路 http://blog.csdn.net/ck_boss

HDOJ 5396 Expression DP

原文:http://blog.csdn.net/ck_boss/article/details/47792355

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!