Probability of staining

dyeing

Title description

There is a two-dimensional table with row R and column C. The row numbers are 0 to R-1 from top to bottom.

The column numbers are 0 to C-1 from left to right, and the two-dimensional table is divided into R*C cells.

Each unit cell is white at the beginning.

Now to execute K instructions, the steps of each instruction are:

(1) Randomly select a cell grid from the two-dimensional table (may be called A grid)

(2) Randomly select a cell grid from the two-dimensional table (may be called B grid)

(3) Grids A and B define a rectangle, and all the cells in the rectangle will be colored.

Each cell is randomly selected, evenly distributed, and each selection is independent of other selections.

A and B may correspond to the same cell.

Your task is: After executing K instructions, how many cells will be dyed? Output the expected value.

Input format

Multiple sets of test data

In the first line, an integer G indicates that there are G groups of test data. 1 <= G <= 6.

Each test data format:

One line, 3 integers: K, R, C. 0 <= K <= 100. 1 <= R, C <= 1000.

Output format

A real number. The error between your output and the standard answer cannot exceed 0.000001.

Input sample

4
1  2  1
2  2  1
1  2  2
3  5  7

Sample output

1.5
1.875
2.25
19.11917924647044

Problem solving ideas

The main idea of ​​the topic: After executing K instructions, find the expected value of the colored cell.

After reading the questions, based on previous experience, we can immediately find some ideas, for example: the probability of drawing two points A and B is (1 R × C) 2 (\frac{1}{R\times C}) ^2(R×C1)2. The size of the rectangle formed by these two points is(∣ x A − x B ∣ + 1) × (∣ y A − y B ∣ + 1) (|x_A-x_B|+1)\times(|y_A-y_B |+1)(xAxB+1)×(yAYB+1)

Based on the above thoughts, we seem to be able to get a violent enumeration approach, but if you think about it, you can find that this simply doesn't work.

At this time, we do not consider the expected value of the grids in the matrix to be colored . We individually consider the probability of each grid being colored , and then add them to find the answer.

How to consider the probability of each grid being colored?


We think about it carefully, what is the probability that a grid will be dyed once.

Suppose now consider a lattice P (x, y) P(x, y)P(x,y ) , how can we choose A and B to make it dyed?

It can be taken like
dyeing
this : The disadvantages of this approach are:

  • There are many and many desirable two points
  • Executing multiple instructions will repeat calculations

We need to find an easy way to solve this problem. I wonder if you have seen the number of goals I wrote :

The probabilities of the above three cases are calculated and added together, and this is the answer. However, there is an easier way.

The probability that at least one team scores a prime number = 1-the probability that both teams score a non-prime number

We can also use the answer=1-non-answer method in this question, which makes the question much simpler.


Consider a lattice P (x, y) P(x, y)P(x,y ) is executingKKThe probabilityofnot being coloredafter K instructions.

Then, the probability that the grid cannot be colored is that the two selected points are selected in the red area:
dyeing
dyeing
dyeing
dyeing
we only need to calculate the probability that A and B will fall in the red area after executing K instructions. Remove the repeated calculation part. The final answer can be obtained.


Code

#include<iostream>
#include<fstream>
#include<cstdio>

using namespace std;
int G,K,R,C;
double s1,s2,s3,s4,s5,s6,s7,s8,prob,temp,temp2;

int main()
{
    
    
	freopen("2805.in","r",stdin);
	freopen("2805.out","w",stdout);
	cin>>G;
	for(int gr=1;gr<=G;gr++)
	{
    
    
		cin>>K>>R>>C;
		prob=0.0;
		for(int i=1;i<=R;i++)
		{
    
    
			for(int j=1;j<=C;j++)
			{
    
    
				s1=(i-1)*C;//above
				s2=(R-i)*C;//under
				s3=R*(j-1);//left
				s4=R*(C-j);//right
				s5=(i-1)*(j-1);//up&left
				s6=(i-1)*(C-j);//up&right
				s7=(R-i)*(j-1);//down&left
				s8=(R-i)*(C-j);//down&right
				/********************两次选点都无法将P(i,j)染上颜色的概率***********************/
				temp2=0.0;
				temp2+=(s1/double(R*C)*s1/double(R*C));
				temp2+=(s2/double(R*C)*s2/double(R*C));
				temp2+=(s3/double(R*C)*s3/double(R*C));
				temp2+=(s4/double(R*C)*s4/double(R*C));
				temp2-=(s5/double(R*C)*s5/double(R*C));
				temp2-=(s6/double(R*C)*s6/double(R*C));
				temp2-=(s7/double(R*C)*s7/double(R*C));
				temp2-=(s8/double(R*C)*s8/double(R*C));
				/********************************************************************************/
				temp=1.00;
				for(int l=1;l<=K;l++)
					temp*=temp2;
				prob+=1.00-temp;
			}
		}
		printf("%.6lf\n",prob);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/bell041030/article/details/89052104