您的位置:首页 > 其它

LightOJ 1284 Lights inside 3D Grid (数学期望)

2017-08-11 14:02 344 查看
题意:在一个三维的空间,每个点都有一盏灯,开始全是关的.现在每次随机选两个点,把两个点之间的全部点,开关都按一遍,问k次过后开着的灯的期望数量;

析:很容易知道,如果一盏灯被按了奇数次,那么它肯定是开的,否则就是关的,所以我们只要计算每盏灯开着的概率就好了。对于每盏灯,假设开一次的概率是p, 这个很容易求得,那么开一共k次有奇数次开着的和是多少呢?假设Fn表示n次奇数的和,那么Fn = Fn * (1-p) + (1-Fn)*p,然后就好算了。解得Fn = 0.5 - 0.5*(1-2p)^n。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100000 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}

double solve(int i, int j){
return (2.0 * i * (j-i+1.0) - 1.0) / j / j;
}

int main(){
int T;  cin >> T;
for(int kase = 1; kase <= T; ++kase){
double ans = 0.0;
int x, y, z;
scanf("%d %d %d %d", &x, &y, &z, &n);
for(int i = 1; i <= x; ++i)  for(int j = 1; j <= y; ++j)
for(int k = 1; k <= z; ++k){
double p = solve(i, x) * solve(j, y) * solve(k, z);
ans += 0.5 - 0.5 * pow(1-2*p, n);
}
printf("Case %d: %.10f\n", kase, ans);
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: