hdu--5396 (interval dp+ permutation combination)

Source: Internet
Author: User


To do this problem, think of whether it is DP, and then found that DP can do, but has been the pit to death.


enumerate the last merged position, and then for the minus, the two parts of the divided into a different combination of methods,
(A1+a2 ...) + (B1,b2 ............) For each A, the number of the added B factorial times, for each B, plus a number of factorial times
subtraction in the same vein
a special point of multiplication
(A1+a2 ...) * (B1,B2) * (...) * * * * ...) Multiply the allocation rate by multiplying the sum of the two parts directly

Think of these are far from the end, because the last two intervals merged, but also involved in a sequence of problems, a good start is silly and naïve directly by 2, the results of hehe.

If there are n symbols, I x on the left, and Y on the right, then because I have enumerated the last position of the merge, the last sign of the operation is determined, and after processing the number of the left and right numbers are added several times, the resulting total number is also processed because the order of merging is different, that is, C ( N-1,X) The relative order of the left-hand symbol in the X-position chosen is the same, because the previous order has been processed. I understand that there is a problem at the beginning, always mixed, especially afraid of DP count, especially with the combination of permutations, feeling the brain a bit worse ...


#pragma COMMENT (linker, "/stack:102400000,102400000") #include <cstdio> #include <cstring> #include < vector> #include <queue> #include <cmath> #include <string> #include <map> #include < utility> #include <algorithm> #include <iostream> #define MEM (b) memset (A,b,sizeof (a)) #define INF 0x3f3f3fusing namespace Std;int dx[]= { -1,0,1,0};///n e s Wint dy[]= {0,1,0,-1};typedef long long ll;typedef pair<int,i nt> pii;const int maxd=200+5;///==============================const int mod=1000000007;ll Dp[maxd][maxd],a[maxd],    Fac[maxd],c[maxd][maxd];int N;char str[maxd];void init () {fac[0]=1;    for (int i=1; i<maxd; i++) Fac[i]=fac[i-1]*i%mod;    MEM (c,0);        for (int i=0; i<maxd; i++) {c[i][0]=1;    for (int j=1; j<=i; j + +) (C[i][j]=c[i-1][j-1]+c[i-1][j])%=mod;    }}ll DFS (int l,int r) {ll &res=dp[l][r];    if (res!=mod) return res;    if (l==r) return res=a[l];    res=0; for (int i=l; I&LT;r;        i++) {ll tmp;        if (str[i]== ' * ') Tmp=dfs (l,i) *dfs (i+1,r)%mod;        else if (str[i]== '-') tmp= (Dfs (l,i) *fac[r-i-1]-dfs (i+1,r) *fac[i-l]+mod)%mod;        else if (str[i]== ' + ') tmp= (Dfs (l,i) *fac[r-i-1]+dfs (i+1,r) *fac[i-l])%mod;    Res= (res+tmp*c[r-l-1][i-l]+mod)%mod; } return res;    int main () {int kase;    Freopen ("In.txt", "R", stdin);    Init ();        while (scanf ("%d", &n)!=eof) {for (int i=0; i<n; i++) scanf ("%lld", &a[i]);        scanf ("%s", str);        for (int i=0, i<n; i++) for (int j=0; j<n; j + +) Dp[i][j]=mod;    cout<< (Dfs (0,n-1) +mod)%mod<<endl; } return 0;} /*33 2 1-+51 4 6 8 3+*-*51 1 1 1 1*****/


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu--5396 (interval dp+ permutation combination)

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.