HDU 5396 Expression

Source: Internet
Author: User

Considering this problem, a hust great God & reflects the lack of his mathematical ability although recently has been relatively busy to write down the problem

After reading the data range first, there should be a lot of people will react to is n^3 DP with F[I][J] to represent the sum of all the conditions from I to J

And then enumerate the intermediate points K from f[i][k] to f[k+1][j] transfer over, but this is not the thought of DP.

--------------------------------------------------------------------------------------------------------------- -----------------

We assume that the left interval in the merge contains the case of A1 A2 ... the right section of the AP contains the situation B1 B2 ... Bq

For multiplication because of the multiplication rate, this property directly multiplies the sum of all the conditions on both sides of the line.

The number of times each element appears on the left side of the addition and subtraction operation is the number of times that each element appears in the right-hand interval P

Further we can find that the number of occurrences p, q is actually equal to (interval length-1)! (exclamation point for factorial do not look wrong = =)

It seems like the thing to do is done, but if you finish writing the code, you'll find that even the sample is too much.

--------------------------------------------------------------------------------------------------------------- ------------------

After thinking about it, we found out that while the right and left intervals are fixed in the merger, the plan to reach the same left and right interval is not unique.

We first consider a relatively small situation, assuming that the left interval through the C1, C2 these two operations to get the right interval through D1, D2 these two operations get

Then we just have to ensure that the order of operation within the same interval can make the last two intervals be the same

such as C1 c2 D1 D2 or C1 d1 C2 d2 or ...

There is a possibility that C (4,2) would be extended to other situations where

C ((left interval length-1) + (right interval length-1) (left interval length-1)) (Note that interval length-1 is the number of operations in the merge process)

So the question was settled happily.

#include <bits/stdc++.h>using namespacestd;Const intn= the, mod=1e9+7;Long LongF[n][n],fac[n],c[n][n];CharS[n];intN;voidprepare () {fac[0]=1;  for(intI=1; i<= -;++i) fac[i]=fac[i-1]*i%MOD; c[0][0]=1;  for(intI=1; i<= -;++i) {c[i][0]=1;  for(intj=1; j<=i;++j) C[i][j]= (c[i-1][j-1]+c[i-1][J])%MOD; }}voidWork () { for(intI=1; i<=n;++i) {scanf ("%lld",&F[i][i]);  for(intj=i+1; j<=n;++j) F[i][j]=0; } scanf ("%s", &s[1]);  for(intlen=2; len<=n;++len) for(intI=1; i+len-1<=n;++i) {intj=i+len-1;  for(intk=i;k<j;++k) {if(s[k]=='*') F[i][j]+=f[i][k]*f[k+1][j]%mod*c[j-i-1][k-i]; Else if(s[k]=='+') F[i][j]+ = (f[i][k]*fac[j-k-1]+f[k+1][j]*fac[k-i])%mod*c[j-i-1][k-i]; ElseF[i][j]+ = (f[i][k]*fac[j-k-1]-f[k+1][j]*fac[k-i])%mod*c[j-i-1][k-i]; F[I][J]%=MOD; }    }    //for (int i=1;i<=n;++i)//for (int j=i;j<=n;++j)//printf ("%d%d%lld\n", i,j,f[i][j]);printf"%lld\n", (f[1][N]+MOD)%MOD);}intMain () {prepare ();  while(~SCANF ("%d",&N)) work (); return 0;}

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