• 2015 Multi-University Training Contest 9 hdu 5396 Expression


    Expression

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


    Problem Description
    Teacher Mai has n numbers $a_1,a_2,dots,a_n$ and $n-1$ operators ("+","-" or "*")$op_1,op_2,dots,op_{n-1}$ which are arranged in the form $a_1quad op_1 quad a_2quad op_2quad a_3dotsquad a_n$

    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(2leq n leq 100)$.

    The second line contains n integers $a_1,a_2,⋯,a_nquad(0≤a_i leq 10^9)$.

    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 $10^9+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
     
    解题:区间动规,尼玛,最坑爹的地方在于区间合并的时候容易把合并也是有时序的忘记乘入了。
     
    c[t-j-1][k-j]就是干这个用的 假设左边的顺序固定,右边的顺序固定,合并后,还得保持来自左边的那些操作符的相对操作顺序,右边的也一样,所以是组合数
     
    关于阶乘,其实这么多个操作符,有多少种操作顺序?当然是阶乘个
     
    至于+-法的合并,可以发现假设A代表里面有3个和的和,B代表里面有2个和的和
     
    A(a,b,c) + B(d+e)
     
    由于我们要求每种可能
     
    那么将产生 
     
    $a + d quad b + d quad c + d$
    $a + e quad b + e quad c + e$
     
    可以发现其等价于 2A + 3B A里面的元素都加了两次,B里面的元素都加了3次
     
    好啦dp吧,没想到要用到$inom{0}{0}$害我debug好久
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const int maxn = 110;
     5 const LL mod = 1000000007;
     6 LL dp[maxn][maxn],c[maxn][maxn] = {1},f[maxn] = {1};
     7 void init() {
     8     for(int i = 1; i < maxn; ++i) {
     9         c[i][0] = c[i][i] = 1;
    10         f[i] = f[i-1]*i%mod;
    11         for(int j = 1; j < i; ++j)
    12             c[i][j] = (c[i-1][j-1] + c[i-1][j])%mod;
    13     }
    14 }
    15 char op[maxn];
    16 int main() {
    17     init();
    18     int n;
    19     while(~scanf("%d",&n)) {
    20         memset(dp,0,sizeof dp);
    21         for(int i = 0; i < n; ++i)
    22             scanf("%I64d",&dp[i][i]);
    23         scanf("%s",op);
    24         for(int i = 2; i <= n; ++i) {
    25             for(int j = 0; j + i <= n; ++j) {
    26                 for(int k = j,t = j + i -1; k < t; ++k) {
    27                     LL tmp;
    28                     if(op[k] == '+')
    29                         tmp = (f[t-k-1]*dp[j][k] + f[k-j]*dp[k+1][t])%mod;
    30                     else if(op[k] == '-') {
    31                         tmp = (f[t-k-1]*dp[j][k] - f[k-j]*dp[k+1][t])%mod;
    32                         tmp = (tmp + mod)%mod;
    33                     } else if(op[k] == '*') tmp = dp[j][k]*dp[k+1][t]%mod;
    34                     tmp = tmp*c[t-j-1][k-j]%mod;
    35                     dp[j][t] = (dp[j][t] + tmp + mod)%mod;
    36                 }
    37             }
    38         }
    39         printf("%I64d
    ",dp[0][n-1]);
    40     }
    41     return 0;
    42 }
    View Code
     
  • 相关阅读:
    做程序员,我骄傲了吗?
    乐字节Java面向对象三大特性以及Java多态
    Java为什么有前途?什么人适合学Java?
    Java新手从入门到精通的学习建议
    Java变量与数据类型之二:Java常量与变量
    模块化、结构化的代码,程序员正讲述着人生
    乐字节Java变量与数据类型之一:Java编程规范,关键字与标识符
    乐字节Java学习课程-path环境变量的作用与配置
    我英语不好,能学会编程吗?
    为什么欧拉图要用栈存然后逆着输出
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4740601.html
Copyright © 2020-2023  润新知