题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3329
参考网址:http://blog.csdn.net/morgan_xww/article/details/6775853
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; const int maxn = 525; double A[maxn],B[maxn]; int sta[maxn],icnt; int n,k1,k2,k3,a,b,c; int main() { //freopen("E:\acm\input.txt","r",stdin); int T; cin>>T; while(T--){ cin>>n>>k1>>k2>>k3>>a>>b>>c; icnt = 0; for(int i=1;i<=k1;i++) for(int j=1;j<=k2;j++) for(int k=1;k<=k3;k++) if(i==a && j==b && k==c) continue; else{ int sum = i + j + k; sta[icnt++] = sum; } for(int i=n+1;i<=maxn;i++) A[i] = B[i] = 0; double p = 1.0/(k1*k2*k3); //又犯傻了。 for(int i=n;i>=0;i--){ A[i] = 1 ; B[i] = 0; for(int j=0;j<icnt;j++){ A[i] += A[i+sta[j]]; B[i] += B[i+sta[j]]; } A[i] *= p; B[i] = p*B[i] + 1; } double ans = B[0]/(1-A[0]); printf("%.15lf ",ans); } }