标签:mem row src cond repr style operator ace can
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 952 Accepted Submission(s): 573
一看这样子就像是区间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\n",f[1][n]); 49 } 50 }
标签:mem row src cond repr style operator ace can
原文地址:http://www.cnblogs.com/zhber/p/7152704.html