LightOJ1284 Lights inside 3D Grid (概率DP)

You are given a 3D grid, which has dimensions XY and Z. Each of the X x Y x Z cells contains a light. Initially all lights are off. You will have K turns. In each of the K turns,

  1. You select a cell A randomly from the grid,
  2. You select a cell B randomly from the grid and
  3. Toggle the states of all the bulbs bounded by cell A and cell B, i.e. make all the ON lights OFF and make all the OFF lights ON which are bounded by A and B. To be clear, consider cell A is (x1, y1, z1) and cell B is (x2, y2, z2). Then you have to toggle all the bulbs in grid cell (x, y, z) where min(x1, x2) ≤ x ≤ max(x1, x2)min(y1, y2) ≤ y ≤ max(y1, y2) and min(z1, z2) ≤ z ≤ max(z1, z2).

Your task is to find the expected number of lights to be ON after K turns.

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

Each case starts with a line containing four integers X, Y, Z (1 ≤ X, Y, Z ≤ 100) and K (0 ≤ K ≤ 10000).

Output

For each case, print the case number and the expected number of lights that are ON after K turns. Errors less than 10-6will be ignored.

Sample Input

5

1 2 3 5

1 1 1 1

1 2 3 0

2 3 4 1

2 3 4 2

Sample Output

Case 1: 2.9998713992

Case 2: 1

Case 3: 0

Case 4: 6.375

Case 5: 9.09765625

题解:

参考代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int T,X,Y,Z,K;
double ans;

double calc(int x,int m)
{
    return 1.0-1.0*((m-x)*(m-x)+(x-1)*(x-1))/(m*m);
}

int main()
{
    scanf("%d",&T);
    for(int cas=1;cas<=T;++cas)
    {
        ans=0;
        scanf("%d%d%d%d",&X,&Y,&Z,&K);
        for(int i=1;i<=X;++i)
            for(int j=1;j<=Y;++j)
                for(int k=1;k<=Z;++k)
                {
                    double p=calc(i,X)*calc(j,Y)*calc(k,Z);
                    ans+=0.5-0.5*pow(1.0-2*p,K);
                }
        printf("Case %d: %.8lf\n",cas,ans);
    } 

    return 0;
}

原文地址:https://www.cnblogs.com/songorz/p/10828658.html

时间: 2024-10-12 04:17:03

LightOJ1284 Lights inside 3D Grid (概率DP)的相关文章

LightOJ 1284 Lights inside 3D Grid

题意是给出一个由边长为1的立方体组成的长方体,每个小立方体中有一盏灯,每次操作随机选择两个立方体,以这两个立方体作为对角线顶点确定一个长方体,长方体中所有块的灯自己取反,就是亮着的关上,暗着的打开,求这样操作k次以后亮着的灯数的期望值. 设F(i)为操作i次以后对某灯奇数次取反的概率,G(i)为偶数次,奇数次取反以后灯是亮着的,贡献的期望值显然是1,所以求出F(k)就可以了. 设p为某灯被选择到的概率. 显然F(i) = F(i - 1) * (1 - p) + G(i - 1) * p, G(

UVA 11605 - Lights inside a 3d Grid(概率+数学)

UVA 11605 - Lights inside a 3d Grid 题目链接 题意:给定一个NxMxP的三维网格,每个格子上一盏灯,现在每次随机选择两点,把这两点构成立方体中间那一块开关灯状态转换,问K步之后网格中亮灯的期望 思路:概率问题,把x,y,z轴分开考虑,算出每一个点xi,yi,zi分别能被选到的情况数,然后根据乘法原理相乘起来除以总情况就能算出一点的概率,然后问题就是K次了,对于K次,每次开到的概率为P的情况下,总情况为∑k1Pi(1?P)k?ii为奇数,那么根据组合公式很容易化

uva 11605 - Lights inside a 3d Grid(概率)

option=com_onlinejudge&Itemid=8&page=show_problem&problem=2652" style="">题目链接:uva 11605 - Lights inside a 3d Grid 题目大意:给定一个三维坐标系大小,每一个位置有一个灯.初始状态为关.每次随机选中两个点,以这两点为对角线的长方体内全部灯转变状态.操作K次.问说平均情况下.最后会有多少栈灯亮着. 解题思路:枚举坐标系上的点.计算单个点亮着

ZOJ3605-Find the Marble(概率DP)

Find the Marble Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice and Bob are playing a game. This game is played with several identical pots and one marble. When the game starts, Alice puts the pots in one line and puts the marble in one of th

HDU 3853 LOOPS (概率dp)

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 2931    Accepted Submission(s): 1209 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help

HDU 4050 wolf5x(动态规划-概率DP)

wolf5x Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 402    Accepted Submission(s): 248 Special Judge Problem Description There are n grids in a row. The coordinates of grids are numbered fro

HDU 3853 概率dp

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 2337    Accepted Submission(s): 951 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl).Homura wants to help he

HDU3853-LOOPS(概率DP求期望)

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 1864    Accepted Submission(s): 732 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help h

LightOJ1030 Discovering Gold 概率DP 水题

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amount of gold. Initially you are in position 1.