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,转载请注明来源
  • 相关阅读:
    Discourse 如何不使用 Let’s Encrypt 而使用 CA 签名的密钥进行安装
    Discourse 重复安装过程中的密钥签发问题
    Discourse 升级后提示 https 混合内容
    CentOS 8 安装 docker 报错 containerd.io >= 1.2.2-3
    MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
    培养自己的5项能力
    高效率工作方式
    项目的架构演进过程
    如何预防后台被攻击,且看Tomcat的安全配置
    redis的缓存更新策略,缓存粒度控制
  • 原文地址:https://www.cnblogs.com/zhber/p/7152704.html
Copyright © 2011-2022 走看看