HDU 5396 interval DP Math Expression

Source: Internet
Author: User

Test instructions: There are n numbers, n-1 operators, the order of each operator can be arbitrary, so there is a total (n-1)! Order of operations, get (N-1)! The results of these operations and the MOD 1e9+7.

Analysis:

The position K of the last operator in an enumeration interval [L, R], which is an analogy to the optimal matrix chain multiplication.

If the operator is multiplicative, then the multiplication is assigned according to the multiplication allocation rate.

There is a total of r-l operators in this interval, where the last operator has been determined to be K, the left interval [L, K] has the K-L operator, the right interval [k + 1, R] has r-k-1 operators.

Moreover, the sequence of the left and right interval operators is determined, and the order between the two intervals is not affected, so the same results are in total C (R-l-1, K-L)

So the answer will be multiplied by this number, D (i, j) + = d (i, K) * d (k + 1, R) * C (R-l-1, L-k) | OP[K] = *

However, if you add and subtract, you cannot directly follow the operator to merge the interval.

For the determination of the left interval a sequence of operations, the right interval has a total (R-k-1)! Result of the operation, so the answer accumulates a D (l, K) * (R-K-1)!

Similarly, for the right interval a definite sequence of operations, the left interval pair should (K-l)! Result of the operation, the answer is summed up a D (k + 1, R) * (L)!

Finally, the order of two interval r-l-1 operators is determined, and the final answer is multiplied by C (R-l-1, K-L)

A final summary of the answer is:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 7typedefLong LongLL;8 9 Const intMAXN = -+Ten;Ten ConstLL MOD =1000000007; One  A intN; - LL A[MAXN]; - LL FAC[MAXN], C[MAXN][MAXN]; the CharOP[MAXN]; -  - intVIS[MAXN][MAXN]; - LL D[MAXN][MAXN]; +  -LL DP (intLintR) + { A     if(Vis[l][r])returnD[l][r]; atll& ans =D[l][r]; -Ans =0; -VIS[L][R] =true; -     if(L = = r)returnAns =A[l]; -     if(L +1==R) -     { in         if(Op[l] = ='*')returnAns = a[l] * A[r]%MOD; -         if(Op[l] = ='+')returnAns = (A[l] + a[r])%MOD; to         if(Op[l] = ='-')returnAns = (((((A[l]-a[r])% MoD) + MoD)%MOD; +     } -      for(intK = l; K < R; k++) the     { *LL T1 = DP (l, k), t2 = DP (k +1, R); $ LL t;Panax Notoginseng         if(Op[k] = ='*') -         { thet = T1 * T2%MOD; +t = T * c[r-l-1][k-l]; AAns = (ans + t)%MOD; the             Continue; +         } -  $T1 = T1 * Fac[r-k-1] %MOD; $t2 = T2 * Fac[k-l]%MOD; -         if(Op[k] = ='+') T = (t1 + t2)%MOD; -         ElseT = ((((t1-t2)% MoD) + MoD)%MOD; thet = T * c[r-l-1][k-l]; -Ans = (ans + t)%MOD;Wuyi     } the  -     returnans; Wu } -  About intMain () $ { -fac[0] =1; -      for(inti =1; i < MAXN; i++) Fac[i] = fac[i-1] * I%MOD; -      for(inti =0; i < MAXN; i++) c[i][0] = C[i][i] =1LL; A      for(inti =2; i < MAXN; i++) +          for(intj =1; J < I; J + +) C[i][j] = (c[i-1][J] + c[i-1][j-1]) %MOD; the  -      while(SCANF ("%d", &n) = =1&&N) $     { the          for(inti =1; I <= N; i++) scanf ("%i64d", A +i); thescanf"%s", OP +1); thememset (Vis,false,sizeof(Vis)); thememset (Vis,0,sizeof(Vis)); -printf"%i64d\n", DP (1, N)); in     } the  the     return 0; About}
code June

HDU 5396 interval DP Math Expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.