zoukankan      html  css  js  c++  java
  • Petrozavodsk Summer Training Camp 2015 Day 2: Xudyh (TooSimple) Contest, Saturday, August 22, 2015 A题

    Problem A.

    Expression Input file: standard input

    Output file: standard output

    Time limit: 1 second

    Memory limit: 64 mebibytes Teacher Mai has n numbers a1, a2, . . . , an and n − 1 operators (each operator is one of ‘+’, ‘-’ 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 ∗ 3 → 1 + 4 ∗ (−2) ∗ 3 → 1 + (−8) ∗ 3 → (−7) ∗ 3 → −21. Input First line of the input contains one integer T (1 ≤ T ≤ 20) — number of the test cases. Then T test cases follow. For each test case, the first line contains one number n (2 ≤ n ≤ 100). The second line contains n integers a1, a2, . . . , an (0 ≤ ai ≤ 109 ). The third line contains a string with length n−1 consisting of ‘+’, ‘-’ and ‘*’, which represents the operator sequence. Output For each test case print the answer modulo 109 + 7. Example standard input standard output 2 3 3 2 1 -+ 5 1 4 6 8 3 +*-* 2 999999689

    题目分析:考虑区间dp dp[i][j]表示i到j的所有可能的组合的和, 考虑转移,<i,j>可以由sigam<i,k>&<k,j>转移;

    对于+或-号,dp[i][j]+=(dp[i][k]*fac[j-k]+dp[k+1][j]*fac[j-k-1])*C[k-i][j-i];

    对于*号 dp[i][j]+=(dp[i][k]*dp[k+1][j])*C[k-i][j-i];

     1 #include<bits/stdc++.h>
     2 #define maxn 310
     3 #define M 100
     4 #define LL long long
     5 #define Mod 1000000007 
     6 using namespace std;
     7 LL fac[maxn],C[maxn][maxn],dp[maxn][maxn];
     8 void pre(){
     9     fac[0]=1;
    10     for(int i=1;i<=M;i++){
    11         fac[i]=(fac[i-1]*i)%Mod;
    12     }
    13     C[0][0]=1;
    14     for(int i=1;i<=M;i++){
    15         C[i][i]=C[i][0]=1;
    16         for(int j=1;j<i;j++){
    17             C[i][j]=(C[i-1][j]+C[i-1][j-1])%Mod;
    18         }
    19     }
    20 }
    21 char op[maxn];
    22 LL seg[maxn];
    23 int n,m;
    24 int main(){
    25     pre();
    26     int T;
    27     cin>>T;
    28     while(T--){
    29     cin>>n;
    30     memset(dp,0,sizeof(dp));
    31     for(int i=1;i<=n;i++){
    32         cin>>dp[i][i];
    33     }
    34     cin>>op;
    35     m=strlen(op);
    36     for(int k=1;k<=n;k++){
    37         for(int i=1;i+k<=n;i++){
    38             for(int j=i;j<i+k;j++){
    39                 if(op[j-1]=='*'){
    40                     dp[i][i+k]=(dp[i][i+k]+(((Mod+dp[i][j]*dp[j+1][i+k])%Mod)*C[k-1][j-i])%Mod)%Mod;
    41                 }    
    42                 else if(op[j-1]=='+'){
    43                     dp[i][i+k]=(dp[i][i+k]+(2*Mod+(dp[i][j]*fac[i+k-j-1])%Mod+(dp[j+1][i+k]*fac[j-i])%Mod)*C[k-1][j-i])%Mod;
    44                 }
    45                 else if(op[j-1]=='-'){
    46                     dp[i][i+k]=(dp[i][i+k]+((2*Mod+(dp[i][j]*fac[i+k-j-1])%Mod-(dp[j+1][i+k]*fac[j-i]))%Mod)*C[k-1][j-i])%Mod;
    47                 }
    48             }
    49         }
    50     }
    51     cout<<(dp[1][n]+Mod)%Mod<<endl;
    52     }
    53     return 0;
    54 }
  • 相关阅读:
    idea注释模板配置
    component-scan中base-package包含通配符
    mysql查询datetime大于下午3点的数据
    Linux c 开发-4 使用QT远程调试Linux程序
    Linux c 开发-3 配置ubuntu子系统桌面环境
    Linux c 开发-2 配置Vs2019
    Linux c 开发-1 Ubuntu子系统18.04开启SSH
    STM32 例程-5 Proteus使用串口2
    STM32 例程-4 Proteus下串口发送数据
    STM32 例程-3 Proteus下单按键试验
  • 原文地址:https://www.cnblogs.com/poler/p/7359464.html
Copyright © 2011-2022 走看看