You are given a 3D grid, which have dimensions N, M and P. Each of the M x N x P cells has a light. Initially all lights are off. You will have K turns. In each of the K turns,
You will select a cell A randomly from the grid
You will select a cell B randomly from the grid
Toggle the states of all the bulbs bounded by cell A and cell B, ie make all the ON lights OFF and make all the OFF lights ON which are bounded by A and B. To be more 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).
How many bulbs are expected to be ON after K turns?
Note:
- A and B can be the same cell.
Input
First line of the input is an integer T(T<101) which denotes the number of test cases. Each of the next T lines represents one test case by 4 integers N, M, P (0 < M, N, P < 101) and K (0<=K<=10000) separated by spaces.
Output
Output one line for each test cases giving the expected number of ON lights. Up to 1E-6 error in your output will be acceptable. Print the case number followed by the output. Look at the sample output for exact format.
Sample Input
|
|
Output for Sample Input
|
|
Solution
題目描述:
在 N x M x P 的三維空間中,任抓 2 個格子所構成的長方體,將長方體內部的所有格子點進行反轉標記 (一開始全是 OFF),抓取 K 次得到的 ON 標記格子數期望值為何?
題目解法:
每一個軸的情況可以分開考慮,考慮對於座標 (x, y, z) 被選到的機率 p,則 sigma(p * 1) = 所求的期望值。
|
|
得到 p 之後,計算在 K 次中,得到奇數 ON 的機率為何,這裡藉由矩陣乘法來完成。
|
|
藉由快速取冪次來完成。
|
|