This question To[i][j] for the first person to send J this gift probability we use 13 to compress this left the number of gifts, so we will dp[i][k] expressed as the first person to put the gift after the state is K, then through the memory of the search we get the state probability that we want to Dp[i][k]+={dp[i+1][k-aj]*to[i][aj]}, well now if I go get the first gift I right probability, we can first know good friend put first probability for
Assuming S is the state of the original gift
Pop=dp[1][s-a1]*to[0][1]/dp[0][s], this is the probability of a good friend sent the first gift, then now for the first gift he has digit "0" We need to have a 1/digit[0] probability to get this gift, And so on to get the 1th 2nd one and so on ...
#include <iostream>#include<cstdio>#include<algorithm>#include<string.h>using namespacestd;Const intmaxn=5;Const intmaxm= -* -* -* -* -+5;intGift[maxn],n;Doubledp[ -][maxm],to[ -][MAXN];BOOLvis[ -][MAXM];intA[MAXN];DoubleDfsintLocintG) { if(Vis[loc][g])returnDp[loc][g]; intk=F; for(intI=4; i>=0; --i) {A[i]= K% -; K/= - ; } Doubleans=0; for(intI=0; i<5; ++i)if(A[i]) {a[i]--; K=0; for(intj =0; j<5; ++j) K= K * -+a[J]; Ans+=dfs (Loc +1, K) *to[Loc] [i]; A[i]++; } Vis[loc][g]=true; returndp[loc][g]=ans;}intMain () {intCAs; scanf ("%d",&CAs); while(CAS--) {scanf ("%d",&N); for(intI=0; i<5; ++i) scanf ("%d",&Gift[i]); for(intI=0; i<n; ++i) for(intj=0; j<5; ++j) scanf ("%LF",&To[i][j]); Memset (DP,0,sizeof(DP)); memset (Vis,false,sizeof(VIS)); vis[n][0]=1; dp[n][0]=1; intk=0; for(intI=0;i<5; ++i) {k=k* -+Gift[i]; } DoubleRes=dfs (0, K); intans=1; Doublepans=-1; for(intI=0; i<5; ++i)if(Gift[i]) {Gift[i]--; K=0; for(intj=0; j<5; ++j) K=k* -+Gift[j]; Gift[i]++; Doublea=dp[1][k]*to[0][i]/gift[i]/Res; if(a>pans) {ans=i+1; pans=A; }} printf ("%d%.3lf\n", Ans,pans); } return 0;}
View Code
uva10417 probability DP