返回二级目录——kaugnbin概率dp习题集
原题
You are given a 3D grid, which has dimensions X, Y and Z. Each of the X x Y x Zcells 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) wheremin(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-6 will 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
题目大意
现在有x*y*z的立方体格子,每个格子有个灯,初始全灭,有d次操作,每次随机选择两点A(x1,y1,z1)和B(x2,y2,z2),使得以A和B为斜对角线的立方体内所有灯的状态翻转,求经过d次操作后亮着的灯的个数期望。
解析
我们考虑每个点贡献的期望=每个点亮着的概率*1
然后把每个点贡献的期望求和即可,这就转化为求每个灯被操作奇数次的概率。
设p为操作一次当前灯改变状态的概率,
对于其中一个坐标(比如x,i),x表示横坐标最大值,i表示当前点的坐标(坐标都从1开始)。则横坐标不能选中当前点i的意思就是选择的两个点的x1,x2在i的同侧,再用x*x减去就是选中的概率。
则:x维度下选中当前点的概率为: func(x,i)=( x*x-(x-i)*(x-i)-i*i )/(x*x)
三个维度都选中当前点才能真正选中当前点,所以把三个维度的概率乘起来才可以,则有:
p=func(x,i)*func(y,j)*func(z,k)
设f(n)为操作n次后当前灯被操作偶数次(灭的)概率,则有地推公式:
f(n)=第n次操作被选中+不选中两种=p*(1-f(n-1)) + (1-p)* f(n-1)
AC代码
#include<bits/stdc++.h>
#include<cstring>
using namespace std;
#define reg register;
int x,y,z,d;
#define lb double
inline long long powf(long long x){
return x*x;
}
inline lb powf(lb x,int b){
lb w=x;
lb ret=1;
while(b)
{
if(b&1){
ret*=w;
}
w*=w;
b>>=1;
}
return ret;
}
inline lb func(int x,int i){
return (powf(x)-powf(i-1)-powf(x-i))/(lb)powf(x);
}
inline lb solve(int i,int j,int k){
lb p=func(x,i);
p*=func(y,j);
p*=func(z,k);
return 1-(pow(1-2*p,d)/2+0.5); //定义f(n)为操作n次,为偶数的概率,f(0)=1
// return (1-powf(1-2*p,d))/2; //定义f(n)为操作n次,为奇数的概率 f(0)=0
}
int main(){
#ifndef ONLINE_JUDGE
freopen("r.txt","r",stdin);
#endif
lb ans;
int T;
int cs=1;
cin>>T;
while(T--){
ans=0;
cin>>x>>y>>z>>d;
for(int i=1;i<=x;i++)
for(int j=1;j<=y;j++)
for(int k=1;k<=z;k++)
ans+=solve(i,j,k);
printf("Case %d: %.7f\n",cs++,ans);
}
}