HDU 5345 Joyful (probability problem seeking expectation)

Source: Internet
Author: User

D-joyfulTime limit:1000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64 U SubmitStatus

Description

Sakura have a very magical tool to paint walls. One day, KAc asked Sakura-to-paint a wall that looks like an $M \times n$ matrix. The wall have $M \times n$ squares in all. The whole problem we denotes $ (x, y) $ to is the square at the $x $-th row, $y $-th column. Once Sakura has determined-squares $ (x_1, Y_1) $ and $ (x_2, y_2) $, she can use the magical tool-to-paint all the square s in the Sub-matrix which have the given and the squares as corners.

However, Sakura is a very naughty girl, so she just randomly uses the tool for $K $ times. More specifically, each time for Sakura to use that tool, she just randomly picks both squares from all the $M \times n$ sq Uares, with equal probability. Now, KAc wants to know the expected number of squares that would be painted eventually.

Input

The first line contains an integer $T $ ($T \le 100$), denoting the number of test cases.

For each test case, the there is only one line, with three integers $M, n$ and $K $.
It is guaranteed, \le M, N \le 500$, \le K \le 20$.

Output

For each test case, the output "case #t:" To represent the $t $-th case, and then output the expected number of squares that would be painted. Round to integers.

Sample Input

23 3 14) 4 2

Sample Output

Case #1:4Case #2:8

Hint the precise answer in the first test case was about 3.56790123.

Test instructions: There are t sets of data, each set of input m,n,k. Represents a m*n matrix that randomly takes two points (x1,y1) in The Matrix, (X2,y2), with the two points as rectangles.

Two vertices, draw a rectangle, that is, the four vertices of the rectangle are (x1,y1), (X1,y2), (X2,y1), (X2,y2). All points in the rectangle are considered to be dyed and K

Times such an operation, ask for the desired number of squares to be stained in the matrix. These two points do not affect each other, that is, the two points can be the same. Each point can be dyed multiple times

Color, is to be dyed two times even two times, not counted once.

The main reason: because (x1,y1), (X2,y2) These two points are taken from the Matrix, the first point has the n*m possibility, the second point also has n*m possibility, so the general situation

Number is n*n*m*m. We discuss each point in the matrix separately, assuming that there is one point, X, Y. We know that x means that the point is on line x, y means that the

Point in column y, then if the two points taken (x1,y1), (x2,y2) are 1 to x-1 rows or between x+1 to M rows or between 1 and y-1 columns or

are between y+1 and N columns, then (x, y) This point will not be dyed, the above four cases can be considered as the upper and lower left and right four kinds of situation. According to the principle of repulsion, we have to

Subtract top left, bottom left, top right, bottom right four cases, this is because the top and left overlay both upper left and so on. The probability that the number of cases is divided by the total number of cases p

is the probability that the point is not dyed, k times the operation, then Tmp=p^k is the point K operation after the probability of not being dyed, 1-tmp is the probability that the point is dyed,

Because the point is a point, so the probability is expected, the expectation of each point added up, is the result, pay attention to rounding with%.0f can be achieved, specific there are many

See the code comment for a number of details that need attention.

#include <iostream>#include<stdio.h>#include<math.h>using namespaceStd;typedefLong Longll;ll C (ll A,ll b) {returna*a*b*b;}intMain () {/*1. Use g++ to submit with C + + 2. Use scanf to write with Cin slowly, although in the same case 3. The TMP with K-th probability is used for cyclic running POW slow*/ll T,n,m,k;//Note that there must be a long long, otherwise the calculation will have to force conversionll Ans,sum; DoubleP,tmp,qiwang; scanf ("%lld",&t);  for(intcas=1; cas<=t;cas++) {scanf ("%lld%lld%lld", &n,&m,&k);//the order of N and M here doesn't matter.qiwang=0;//Qiwang says the probability that the lattice is dyed is expected, because N is 1.p=0;//p indicates the probability that the lattice is not dyed after one operationtmp=1;//TMP indicates the probability that the lattice is not dyed after K operationSum=n*n*m*m;//divide by sum to find the probability//Note that the following code, N and M, is reversed, but has no effect on the answer.         for(intI=1; i<=n;i++)//a discussion of each lattice         for(intj=1; j<=m;j++) {ans=0;//InitializeAns+=c (I-1, M); Ans+=c (ni,m); Ans+=c (J-1, N); Ans+=c (M-j,n); Ans-=c (I-1, J-1); Ans-=c (I-1, M-j); Ans-=c (J-1, N-i); Ans-=c (N-I,M-J);//principle of tolerance and repulsionp=1.0*ans/sum;//the probability of the lattice not being dyedtmp=1.0;//Initialize             for(intx=1; x<=k;x++)//Better than POW.Tmp*=p;//the probability that the lattice will not be dyed after k-Timesqiwang=qiwang+1-tmp;//the probability that the lattice is dyed is expected} printf ("Case #%d:%.0f\n", Cas,qiwang);//%0.F Automatic rounding of floor or +0.5 or round functions can also be    }    return 0;}

HDU 5345 Joyful (probability problem seeking expectation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.