- 思路:
- 因为各个点的结果是互相独立的,所以最终的期望等于各个点的期望之和。
- 分别考虑每个点的亮灯的概率:先设一个点取到的概率为P,设dp[i]为该点的在i轮亮着的概率,则可以推得dp[i] = dp[i-1]*(1-p) + p*(1-dp[i-1]);
则可推得dp[i] = dp[i-1]*(1-2*p)+p;
下面构造一个等比数列:dp[i]-0.5=(dp[i-1]-0.5)*(1-2*p);
=> dp[i] = (dp[1] - 0.5)*(1-2*p)^(i-1)+0.5;
=> dp[i] = (1 - (1-2*p)^i)/2; - 而对于每个点被取到的概率,先分别考虑该点对应的坐标x,y,z的概率,设函数P(x,n)为在坐标范围为n的轴上取到x的概率,则P(x,n) = (2*((x-1)/n)*((n-x+1)/n) - 1)/(n^2);对于该点被取到的概率,等于将P(x,max_x)*P(y,max_y)*P(z,max_z);
- 接下来只需要遍历一遍所有的点,将所有点的期望相加即可,代码如下:
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <cctype>
#include <cmath>
#include <cstring>
#include <climits>
#include <complex>
#include <set>
#include <deque>
#define DEBUG(x) cerr<<"line:"<<__LINE__<<", "<<#x" == "<<(x)<<endl;
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define FOR(it,s) for(__typeof(s.begin()) it=s.begin();it!=s.end();it++)
#define ALL(a) a.begin(),a.end()
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define DRI(x) int (x);scanf("%d",&(x))
#define DRII(x,y) int (x),(y);scanf("%d%d",&(x),&(y))
#define DRIII(x,y,z) int (x),(y),(z);scanf("%d%d%d",&(x),&(y),&(z))
#define MS0(a) memset((a),0,sizeof((a)))
#define MS1(a) memset((a),-1,sizeof((a)))
#define MS(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
typedef unsigned int uint;
typedef unsigned long long ULL;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<vi> vvi;
#define INF 1000000000
const double eps = 1e-10;
int dcmp(double x){
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
int N = 100;
int n, m, h, k;
double p(int pos,int n){
return (2.0 * (n-pos+1)*pos*1.0 - 1) / (n * n);
}
double solve(double p){
return ( 1.0 - pow(1-2*p,k)) / 2.0;
}
int main(void)
{
#ifdef LOCAL
#endif
DRI(t);
for(int kase = 1;kase <= t; kase++){
double res = 0;
RII(n,m);RII(h,k);
for(int i = 1; i <= n; i++){
double px = (p(i,n));
for(int j = 1; j <= m;j ++){
double py = p(j,m);
for(int tmp = 1; tmp <= h; tmp++){
double pz = p(tmp, h);
res += solve(px * py * pz);
}
}
}
printf("Case %d: %lf\n", kase, res);
}
#ifdef LOCAL
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}