zoukankan      html  css  js  c++  java
  • hdu5396 Expression

    Expression

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


    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(0ai10^9) .

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

    一看这样子就像是区间dp

    再看看数据肯定是区间dp

    f[i][j]表示区间[i,j]一共(j-i)!种运算得到的所有数之和

    加减都很简单,枚举区间[i,j]中i到j-1中间最后一个运算符k

    如果是加号,f[i][j]+=(f[i][k]*(j-k-1)!+f[k+1][j]*(k-i)!)*C(j-i-1,k-i)

    意思就是考虑左右两边的f[i][k],f[k+1][j]对f[i][j]的影响

    如果是减号,把上面+改-

    乘法不会,orz了某神犇之后才知道

    f[i][j]+=f[i][k]*f[k+1][j]*C(j-i-1,k-i)

     1 #include<cstdio>
     2 #include<cstring>
     3 #define LL long long
     4 #define mod 1000000007
     5 inline LL read()
     6 {
     7     LL x=0,f=1;char ch=getchar();
     8     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     9     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    10     return x*f;
    11 }
    12 int c[110][110];
    13 LL a[110];
    14 char s[110];
    15 LL f[110][110];
    16 LL jc[110];
    17 inline void init()
    18 {
    19     c[1][1]=1;
    20     for (int i=0;i<=100;i++)c[i][0]=1;
    21     for (int i=1;i<=100;i++)
    22         for (int j=1;j<=i;j++)
    23         c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
    24     jc[0]=1;
    25     for (int i=1;i<=100;i++)jc[i]=jc[i-1]*i%mod;
    26 }
    27 int n;
    28 int main()
    29 {
    30     init();
    31     while (~scanf("%d",&n))
    32     {
    33         memset(f,0,sizeof(f));
    34         for (int i=1;i<=n;i++)a[i]=read();
    35         scanf("%s",s+1);
    36         for (int i=1;i<=n;i++)f[i][i]=a[i];
    37         for (int len=1;len<=n;len++)
    38             for (int i=1;i<=n;i++)
    39             {
    40                 int j=i+len-1;if (j>n)break;
    41                 for (int k=i;k<j;k++)
    42                 {
    43                     if (s[k]=='+')f[i][j]=(f[i][j]+(f[i][k]*jc[j-k-1]+f[k+1][j]*jc[k-i])%mod*c[j-i-1][k-i]%mod)%mod;
    44                     if (s[k]=='-')f[i][j]=(f[i][j]+(f[i][k]*jc[j-k-1]-f[k+1][j]*jc[k-i])%mod*c[j-i-1][k-i]%mod+mod)%mod;
    45                     if (s[k]=='*')f[i][j]=(f[i][j]+(f[i][k]*f[k+1][j])%mod*c[j-i-1][k-i])%mod;
    46                 }
    47             }
    48         printf("%lld
    ",f[1][n]);
    49     }
    50 }
    hdu 5396
    ——by zhber,转载请注明来源
  • 相关阅读:
    JavaScript深入之参数按值传递
    计算机网络:这是一份全面 & 详细 的TCP协议学习指南
    前端点击下载excel表格数据
    为什么选择器:last-child有时没有起作用?
    深入理解防抖和节流函数
    收集常用正则表达式
    深入研究-webkit-overflow-scrolling:touch及ios滚动
    一文搞懂网络知识,IP、子网掩码、网关、DNS、端口号
    正则替换replace中$1的用法
    数据库连接池性能对比
  • 原文地址:https://www.cnblogs.com/zhber/p/7152704.html
Copyright © 2011-2022 走看看