Light oj 1284 Lights inside 3D Grid

探讨三维格子中随机选择两点并翻转其连线格子状态k次后,状态为1的格子期望数量计算方法。通过分析单个格子被翻转概率及其数学模型,给出具体实现代码。

题目链接:http://acm.hust.edu.cn/vjudge/problem/26994


题意:有一个x*y*z的三维格子,每次随机选择两个点,并且使得两个点之间的所有格子状态取反,求翻转k次后,状态为1的格子的期望数目。


思路:单独考虑每一个格子翻转k次后状态为1的概率,然后累加每一个格子的概率就是期望。对于一个格子(x0,y0,z0),分别考虑每一维,如果想要翻转它一次在x维上选择两个坐标的方案总数为2*x0*(x-x0+1)-1,这两个点可以分别选择在[1,x0]和[x0,x]之间,但是都选在x0处会算重一次。其他维上的坐标同理。这里看别人的思路设f(i)表示i次操作后操作了偶数次的概率,g(i)表示i次操作后操作了奇数次的概率。g(1) = p,f(1) = 0,f(i+1) = p*g(i) + (1-p)*f(i)  g(K)可以推出O(1)的公式。


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)
#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod %100000007
int T;
int x,y,z,K;
int f(int pos,int t)
{
    return 2*pos*(t-pos+1)-1;
}

int main()
{
    cin>>T;
    rep(kase,1,T)
    {
        scanf("%d%d%d%d",&x,&y,&z,&K);
        double ans = 0;
        LL tot = (LL)x*x*y*y*z*z;
        rep(i,1,x)
            rep(j,1,y)
                rep(k,1,z)
                {
                    double p = (double)f(i,x)*f(j,y)*f(k,z)/tot;
                    ans+= (0.5 - 0.5*pow(1-2*p,(double) K));
                }
        printf("Case %d: %lf\n",kase,ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值