LightOJ 1284 - Lights inside 3D Grid 概率/期望/二项式定理

本文介绍了一种算法,用于计算在给定次数内操作长方体内部灯开关后,亮灯数量的期望值。通过计算每个灯被选中操作的概率,并结合操作次数,最终得出亮灯数目的期望值。


题意:给你一个长宽高为x,y,z的长方体,里面每个格子放了灯,再给你k次选取任意长方体形状的区块,对其内所有灯开或关操作,初始为关,问亮灯数量的期望值。
题解:首先考虑选取区块的概率,使某个灯在被选取的区块内要求为三维的每个坐标都在选取范围内如 \(x1<=x0<=x2\)
一个维度为选中的情况总数为\[Q_{x} = x^2 - (a-1)^2 - (x-a)^2 \]
所以一个格点被选到的概率为\(p=\frac{Q_{x}Q_{y}Q_{z}}{{x^2}{y^2}{z^2}} \)
再者考虑奇数次被开启是开 偶数次开启是关,所以
\[p_{奇}=\sum_{i mod 2 = 1}^{n}{C_{k}^{i}p^{i}(1-p)^{k-i}} \]
\[p_{偶}=\sum_{2 | i}^{n}{C_{k}^{i}p^{i}(1-p)^{k-i}}\]
写出偶数项后可发现两个函数和就是二项式公式 \(p_{奇} + p_{偶} = (1 - p + p)^k = 1\)
再考虑到奇数这个特殊性质
\(p_{偶} - p_{奇} = (1 - p - p)^k \)
然后求得 \[p_{奇}=\frac{1-(1-2p)^k}{2}\]
然后期望值就等于全部选取方案下的亮灯概率和。


/** @Date    : 2016-10-25-19.26
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @Link    : 
  * @Version : $
  */
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
#include <math.h>
#define LL long long
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+2000;

double cal(double a, double x)
{
    return (x * x - (a-1) * (a-1) - (x-a) * (x-a))/(x * x);
}


int main()
{
    int T;
    int cnt = 0;
    cin >> T;
    while(T--)
    {
        double x, y, z, K;
        scanf("%lf%lf%lf%lf", &x, &y, &z, &K);
        double ans = 0;
        for(int i = 1; i <= x; i++)
        {
            for(int j = 1; j <= y; j++)
            {
                for(int k = 1; k <= z; k++)
                {
                    double p = cal((double)i, x)*cal((double)j, y)*cal((double)k, z);
                    ans += (1 - pow(1 - 2 * p, K)) / 2;
                }
            }
        }
        printf("Case %d: %.10lf\n", ++cnt, ans);
    }
    return 0;
}

转载于:https://www.cnblogs.com/Yumesenya/p/6008639.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值