标签:des style blog http color os io java strong
Description
Input
Output
Sample Input
1 2
Sample Output
3.0000
题意及分析:
转自:http://blog.csdn.net/morgan_xww/article/details/6774708
dp求期望的题。
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<vector> 10 #include<set> 11 12 #define N 1005 13 #define M 100000 14 #define inf 1000000007 15 #define mod 1000000007 16 #define mod2 100000000 17 #define ll long long 18 #define maxi(a,b) (a)>(b)? (a) : (b) 19 #define mini(a,b) (a)<(b)? (a) : (b) 20 21 using namespace std; 22 23 int n; 24 int s; 25 double dp[N][N]; 26 double p1,p2; 27 28 void ini() 29 { 30 memset(dp,0,sizeof(dp)); 31 p1=(1.0)*1/n; 32 p2=(1.0)/s; 33 // printf(" %.4f %.4f\n",p1,p2); 34 } 35 36 void solve() 37 { 38 int i,j; 39 for(i=n;i>=0;i--){ 40 for(j=s;j>=0;j--){ 41 if(i==n && j==s) continue; 42 dp[i][j]=1+dp[i+1][j]*p1*(n-i)*p2*j+dp[i][j+1]*p1*i*p2*(s-j) 43 +dp[i+1][j+1]*p1*(n-i)*p2*(s-j); 44 dp[i][j]/=(1-p1*i*p2*j); 45 } 46 } 47 48 // for(i=n;i>=0;i--){ 49 // for(j=s;j>=0;j--){ 50 // printf(" i=%d j=%d dp=%.4f\n",i,j,dp[i][j]); 51 // } 52 //} 53 } 54 55 void out() 56 { 57 printf("%.4f\n",dp[0][0]); 58 } 59 60 int main() 61 { 62 //freopen("data.in","r",stdin); 63 //freopen("data.out","w",stdout); 64 //scanf("%d",&T); 65 // for(int cnt=1;cnt<=T;cnt++) 66 // while(T--) 67 while(scanf("%d%d",&n,&s)!=EOF) 68 { 69 ini(); 70 solve(); 71 out(); 72 } 73 return 0; 74 }
标签:des style blog http color os io java strong
原文地址:http://www.cnblogs.com/njczy2010/p/3949322.html