I think the train of thought is correct. I have not found any mistakes. Please enlighten me.
The Code is as follows:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)const int mod=100000000;const int INF=0x3f3f3f3f;const double eqs=1e-8;double dp[2001][2001];int main(){ int hp1, hp2, i, j; double a[8], b[8], w1, w2, tmp, p; while(scanf("%d%d",&hp1,&hp2)!=EOF) { for(i=0; i<6; i++) { scanf("%lf",&a[i]); } for(i=0; i<6; i++) { scanf("%lf",&b[i]); } w1=w2=0; for(i=1; i<6; i++) { for(j=0; j<i; j++) { w1+=a[i]*b[j]; w2+=b[i]*a[j]; } } p=w1+w2; if(p) { w1/=p; w2/=p; } for(i=hp1; i>0; i--) { for(j=hp2; j>=0; j--) { if(i==hp1&&j==hp2) { dp[i][j]=1; } else if(i==hp1) { dp[i][j]=dp[i][j+1]*w1; } else if(j==hp2) { dp[i][j]=dp[i+1][j]*w2; } else if(!j) { dp[i][j]=dp[i][j+1]*w1; } else { dp[i][j]=dp[i+1][j]*w2+dp[i][j+1]*w1; } } } double ans=0; for(i=1; i<=hp1; i++) { ans+=dp[i][0]; } printf("%.6f\n",ans); } return 0;}
HDU 3076 ssworld vs DDD (probability DP)