题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3593

  带环的概率DP一般的做法是求出转移方程,然后高斯消元解方程。但是这里的环比较特殊,都是指向f[0]。

  此题的转移方程为:f[i]=Σ(f[i+k]*p[k])+f[0]*p[0]+1.

  我们可以设 f[i]=A[i]*f[0]+B[i].带入右边有:

                f[i]=Σ(A[i+k]*f[0]*p[k]+B[i+k]*p[k])+f[0]*p[0]+1.

              ->  f[i]=Σ(A[i+k]*p[k]+p[0])*f[0]+B[i+k]*p[k]+1.

  可以得到A[i]=A[i+k]*p[k]+p[0],B[i]=B[i+k]*p[k]+1,退出A[0]和B[0]就可以得到f[0]了。

 //STATUS:C++_AC_0MS_196KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End double p[];
double A[N],B[N];
int T,n; int main(){
// freopen("in.txt","r",stdin);
int i,j,k;
int k1,k2,k3,a,b,c;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d%d%d%d",&n,&k1,&k2,&k3,&a,&b,&c);
mem(p,);
for(i=;i<=k1;i++){
for(j=;j<=k2;j++){
for(k=;k<=k3;k++){
if(i==a && j==b && k==c)p[]=;
else p[i+j+k]+=;
}
}
}
for(i=;i<=k1+k2+k3;i++)p[i]/=k1*k2*k3;
for(i=n;i>=;i--){
A[i]=p[],B[i]=;
for(j=;j<=k1+k2+k3 && i+j<=n;j++){
A[i]+=A[i+j]*p[j];
B[i]+=B[i+j]*p[j];
}
} printf("%.15lf\n",B[]/(-A[]));
}
return ;
}
05-08 07:59