标签:
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<vector> #include<queue> #include<cmath> using namespace std; #define INF 0x3fffffff #define maxn 110 typedef long long LL; const LL MOD = 1e9+7; LL A[maxn], C[maxn][maxn]; char op[maxn]; LL dp[maxn][maxn]; int main() { int n; A[0] = 1; for(int i=1; i<=100; i++) A[i] = (A[i-1] * i)%MOD; C[0][0] = 1; for(int i=1; i<=100; 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; } while(scanf("%d", &n) != EOF) { memset(dp, 0, sizeof(dp)); for(int i=1; i<=n; i++) scanf("%I64d", &dp[i][i]); scanf("%s", op+1); for(int L=2; L <= n; L++) { for(int i=1; i+L-1 <= n; i++) { int j = i + L - 1; dp[i][j] = 0; for(int k=i; k<j; k++) { LL t; if(op[k] == ‘*‘) t = (dp[i][k] * dp[k+1][j])%MOD; if(op[k] == ‘+‘) t = (dp[i][k]*A[j-k-1] + dp[k+1][j]*A[k-i])%MOD; if(op[k] == ‘-‘) t = (dp[i][k]*A[j-k-1] - dp[k+1][j]*A[k-i])%MOD; dp[i][j] = (dp[i][j] + t * C[j-i-1][k-i])%MOD; } } } printf("%I64d\n", (dp[1][n]+MOD)%MOD ); } return 0; }
HDU 5396 Expression(DP+组合数)(详解)
标签:
原文地址:http://www.cnblogs.com/chenchengxun/p/4741439.html