括号
/* 动态转移方程f[i][j]=(f[i+1][j-1]+g[i-1][j-1])*l[i][z]*r[j][z]; f[i][j]是区间[i+1,j-1]匹配的概率与i,j处匹配的概率; g[i][j]是区间[i,j]以()(())...()形式匹配的概率; 答案f[1][n]+g[1][n]; */ #include<cstdio> int n,k; double f[201][201],g[201][201],l[201][51],r[201][51]; int main(){ freopen("brackets.in","r",stdin); freopen("brackets.out","w",stdout); scanf("%d%d",&n,&k); if(n==0){printf("1.00000\n");fclose(stdin);fclose(stdout);return 0;} for(int i=1;i<=n;i++) for(int j=1;j<=k;j++) scanf("%lf%lf",&l[i][j],&r[i][j]); for(int i=0;i<=n;i++) f[i+1][i]=1; for(int len=2;len<=n;len+=2) for(int i=1;i<=n-len+1;i++){ for(int as=1;as<=k;as++) f[i][i+len-1]+=(f[i+1][i+len-2]+g[i+1][i+len-2])*l[i][as]*r[i+len-1][as]; for(int mid=i+1;mid<=i+len-2;mid+=2) g[i][i+len-1]+=f[i][mid]*(f[mid+1][i+len-1]+g[mid+1][i+len-1]); } printf("%.5lf\n",f[1][n]+g[1][n]); fclose(stdin); fclose(stdout); return 0; }