码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5396 区间DP

时间:2015-08-21 23:30:30      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:acm   hdu   

Expression

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


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


#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;
#define LL long long
const int mod = 1000000000 + 7;
const int N = 105;

LL d[N][N];
char s[N];
int n;
LL C[N][N];
LL A[N];

void doit()
{
    A[0] = 1;
    for(int i = 1; i <= 100; i++)
    A[i] = (A[i - 1] * i) % mod;

    memset(C, 0, sizeof C);
    for(int i = 0; i <= 100; i++)
    {
        C[i][0] = 1;
        for(int j = 1; j <= i; j++)
        C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
    }
}

int main()
{
    doit();
    while(~scanf("%d", &n))
    {
        for(int i = 1; i <= n; i++) scanf("%I64d", &d[i][i]);
        scanf("%s", s + 1);

        for(int L = 1; L <= n; L++)
        for(int i = 1; i + L <= n; i++)
        {
            int j = i + L;
            d[i][j] = 0;
            for(int k = i; k < j; k++)
            {
                LL tmp;
                if(s[k] == '*') tmp = (d[i][k] * d[k + 1][j]) % mod;
                else if(s[k] == '+') tmp = (d[i][k] * A[j - k - 1] + d[k + 1][j] * A[k - i]) % mod;
                else tmp = (d[i][k] * A[j - k - 1] - d[k + 1][j] * A[k - i]) % mod;
                d[i][j] = (d[i][j] + tmp * C[j - i - 1][k - i]) % mod;
            }
        }
        printf("%I64d\n", (d[1][n] + mod) % mod);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 5396 区间DP

标签:acm   hdu   

原文地址:http://blog.csdn.net/dojintian/article/details/47841849

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!