Problem A
Lights inside a 3D Grid
Input: Standard Input
Output: Standard Output
You are given a 3Dgrid, which have dimensions N, M and P. Each of the M xN x P cells has a light. Initially all lights are off. You will have Kturns. In each of the K turns,
- Youwill select a cell A randomly from the grid
- Youwill select a cell B randomly from the grid
- Togglethe states of all the bulbs bounded by cell A and cell B, ie make all the ONlights OFF and make all the OFF lights ON which are bounded by A and B. To bemore clear, consider cell A is (x1, y1, z1) and cell B is (x2, y2, z2). Thenyou have to toggle all the bulbs in grid cell (x, y, z) wheremin(x1,x2)<=x<=max(x1,x2) , min(y1,y2)<=y<=max(y1,y2) and min(z1,z2)<=z<=max(z1, z2).
How many bulbs areexpected to be ON after K turns?
Note:
- A and B can be the samecell.
Input
First line of the input is an integer T(T<101)which denotes the number of test cases. Each of the next T linesrepresents one test case by 4 integers N, M, P (0 < M, N, P < 101) andK (0<=K<=10000) separatedby spaces.
Output
Output one line for each test casesgiving the expected number of ON lights. Up to 1E-6 error in your output willbe acceptable. Print the case numberfollowed by the output. Look at thesample output for exact format.
Sample Input Outputfor Sample Input
2 2 3 4 1 | Case 1: 6.3750000000 Case 2: 9.0976562500 |
这题挺好~
有一个M*N*P的立方体,每次随机选两个格子,把它们之间的灯全打开(如果已经是打开的就关闭),经过K次操作之后开着的灯的个数的期望是多少。
把每个灯经过K次操作亮着的概率P求出来,因为每个灯是独立的,最后的期望也就是ΣPi*1+(1-Pi)*0。
对于一个灯来说, 设f[i]是经过i次操作亮着的概率,g[i]是经过i次操作不亮的概率,则有f[i]+p[i]=1,f[i]=f[i-1]*(1-p)+g[i-1]*p,这里的p是操作一次能操作到这个灯的概率。
通过这两个式子,能得到f[i]=f[i-1]*(1-2p)+p,两边加上b/(a-1),也就是0.5,成为等比数列,最后得到f[i]=0.5-0.5*(1-2p)^i。
那么只要对每个灯求出p就能算出答案了。能操作到这个灯等价于选的两个点的坐标在x轴,y轴,z轴都分别在这个灯坐标的两侧,对每个轴分别算符合的情况,然后相乘,最后除以所有情况就是概率。
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<stack>
#define INF 0x3f3f3f3f
#define MAXN 1000010
#define MAXM 110
#define MOD 1000000007
#define MAXNODE 4*MAXN
#define eps 1e-9
using namespace std;
typedef long long LL;
int T,X,Y,Z,K;
double cal(int a,int b){
return (double)b*b-(double)(a-1)*(a-1)-(double)(b-a)*(b-a);
}
int main(){
freopen("in.txt","r",stdin);
scanf("%d",&T);
int cas=0;
while(T--){
scanf("%d%d%d%d",&X,&Y,&Z,&K);
double ans=0,all=(double)X*X*Y*Y*Z*Z;
for(int i=1;i<=X;i++)
for(int j=1;j<=Y;j++)
for(int k=1;k<=Z;k++){
double p=cal(i,X)*cal(j,Y)*cal(k,Z)/all;
ans+=0.5-pow(1-2*p,K)/2;
}
printf("Case %d: %.10lf\n",++cas,ans);
}
return 0;
}