We are to compute probability that x+y <= C, where x is uniformly chosen from [0, A] and y uniformly from [0, B] independently (presumably continuous uniform distributions). So probability is area of region { (x,y) : 0 ≤ x ≤ A, 0 ≤ y ≤ B, x+y ≤ C } divided by total area A*B (if A>0 and B>0). But we have edge cases when A=0 or B=0. Let's treat them carefully.
We have 0 ≤ A,B ≤ 10^9, C ≥ 0 up to 1e18. Since A and B are at most 1e9, C may be larger than A+B. So probability is essentially min(1, something). But careful: x and y are real numbers. So region is a rectangle [0,A] x [0,B] intersected with half-plane x+y ≤ C.
The area depends on C relative to A and B. Standard problem.
If C ≤ 0? Since C ≥ 0, but it could be 0. Then if C=0, region requires x+y ≤ 0, but x,y ≥ 0, so only point (0,0) => area 0 => probability 0, unless A=0 and B=0? Actually if A=0, x is fixed at 0; similarly B=0, y fixed at 0; then x+y=0 ≤ 0, so probability 1. So need to handle degenerate rectangles (line or point). So careful.
General approach: The total "area" of sample space is A*B if A>0 and B>0. If either is zero, then it's degenerate. Actually when A=0, x is always 0; similarly B=0. Then probability is essentially whether condition holds for that single point(s). But if A=0 and B=0, sample space is a point, probability 1 if condition holds (which it does when C≥0). But if A=0, B>0, then x=0 always, y ∈ [0,B] uniformly. Then probability = probability that y ≤ C, i.e., min(1, C/B) if C≥0? But careful: condition is 0 ≤ y ≤ B, and x=0, so x+y=y ≤ C. So we need to compute probability that y ≤ C. However y is continuous uniform on [0,B]. So probability = min(max(0, C)/B, 1) if C>=0? Actually if C<0 would be 0, but C≥0 so non-negative. So probability = min(C / B, 1). But also if B=0, then y=0 always; then probability = 1 if 0≤C, else 0, but C≥0 so 1. So indeed.
If B=0 similarly.
But they said A,B are integers? Yes, given as integers but x,y are real numbers. So we treat them as real lengths. So area computations use A and B as lengths.
Now for non-degenerate case: A>0, B>0.
Region area:
If C ≥ A + B: whole rectangle => area = A * B.
If C ≤ 0: area = 0 (but C≥0, so only C=0 gives area maybe 0 if both A and B >0? Actually if C=0, then x+y≤0 => x=y=0 only, so area = 0; so 0).
If 0 ≤ C ≤ min(A,B)? Actually typical shape: the line x+y=C cuts the rectangle. There are three regimes:
- C ≤ 0: area=0 (not relevant)
- 0 ≤ C ≤ min(A,B): Then the line from (0,C) to (C,0) lies entirely within the rectangle, so area = (C^2)/2 (right triangle with legs C)
- min(A,B) < C ≤ max(A,B): Suppose A ≤ B? Actually need to treat general. The shape is a right triangle plus a rectangle? Let's derive.
Consider rectangle [0,A] × [0,B]. The region x+y ≤ C is the region below the line y = C - x.
For x from 0 to A, y max is min(B, C - x) provided C - x ≥ 0, else 0.
So area = ∫_{x=0}^{A} min(B, max(0, C-x)) dx.
Breakpoint where C-x = B => x = C - B. Also where C-x = 0 => x = C.
So we have x intervals:
- x from 0 to max(0, C-B): if C > B, then for x up to C-B, C-x ≥ B, so min(B, C-x) = B. So contributes B * length of that interval.
- x from max(0, C-B) to min(A, C): here C-x between 0 and B, so min = C-x. So integrate (C-x) dx.
- x from min(A, C) to A: here C-x ≤ 0, so min=0.
But careful: The lower bound of integration for the second part should be max(0, C-B) and upper bound min(A, C). Also the first part exists only if C > B and also the interval [0, C-B] overlaps with [0,A]. So the actual intervals:
Let x1 = max(0, C-B) ; x2 = min(A, C). The integration from 0 to A:
If x2 ≤ 0? Since C≥0, min(A,C)≥0? Actually if C=0 then min(A,0)=0, so x2=0. So the region is zero.
Otherwise, we have:
- For 0 ≤ x ≤ x1 (provided x1>0), y_max = B.
- For x1 ≤ x ≤ x2, y_max = C - x.
- For x ≥ x2, y_max = 0.
Now x1 = max(0, C-B). But also we must ensure x1 ≤ x2, otherwise if C ≤ B then C-B ≤0 => x1=0, so first interval empty. So indeed first interval appears only when C > B.
Similarly, second interval exists only if x2 > x1, i.e., if min(A, C) > max(0, C-B). This is typical.
So area =
if C ≤ 0: 0.
Else if C ≥ A+B: full area A*B.
Else:
We compute region area =
If C ≤ A and C ≤ B: then area = C^2/2.
If C > A and C > B: then area = AB - (A+B-C)^2/2? Actually that's when C is between max(A,B) and A+B? Wait if C > A and C > B, then both A and B are less than C, but C might still be less than A+B. In that case the line cuts off a corner from the rectangle, leaving area = AB - ( (A+B-C)^2 / 2 ). Because the region where x+y > C is a small triangle at the top right corner, with legs (A+B-C). That triangle area = ((A+B-C)^2)/2. So area = A*B - that.
But if C is between A and B (assuming A ≤ B), then shape is trapezoid: For x from 0 to something. Let's derive systematically:
Case 1: C ≤ min(A,B): area = C^2/2.
Case 2: min(A,B) < C ≤ max(A,B): Suppose A ≤ B. Then:
- If C between A and B: i.e., A ≤ C ≤ B. Then for x from 0 to A, y_max = min(B, C-x). Since C ≤ B, we have C-x ≤ C ≤ B for all x? Actually if C ≤ B, then C-x ≤ C ≤ B, so min(B, C-x)=C-x as long as C-x ≥0; else 0. So the line crosses the rectangle at x from 0 to C (but we only go to A because A ≤ C). So x range: [0, A] all have C-x ≥ 0? Since C ≥ A, for x up to A, C-x ≥ C-A ≥ 0. So yes, all x in [0,A] have y_max = C-x. So area = ∫_{0}^{A} (C-x) dx = CA - A^2/2. That's a trapezoid (or a right triangle plus rectangle). Actually shape: rectangle width A, but the top is sloping from y=C at x=0 to y=C-A at x=A. So area = AC - A^2/2.
Similarly if B ≤ A and C between B and A: area = C*B - B^2/2.
Case 3: C > max(A,B) but C < A+B: then area = A*B - ((A+B-C)^2)/2.
But need to confirm these formulas handle boundaries correctly. For instance, if C = min(A,B), case1 gives area = (min)^2/2. In case2, when C = A (assuming A ≤ B) gives area = AA - A^2/2 = A^2/2, consistent. If C = max(A,B) (but < A+B), say A ≤ B, C = B. Then case2 formula for C between A and B? But if C = B and A ≤ B, we are in case2 (since C = max(A,B)? Actually max is B, so C = B, which might be the boundary between case2 and case3 if B < A+B? It is within case2 if we considered min < C ≤ max. Yes, when C = B (max) and A ≤ B, then area from case2: area = CA - A^2/2 = BA - A^2/2. On the other hand, case3 when C = A+B? Actually case3 covers C between max(A,B) and A+B, inclusive? At C = max(A,B) the triangle (A+B-C) = A+B - max. For A≤B, A+B-B = A. So area from case3 would be AB - (A^2)/2 = AB - A^2/2, same as case2. So continuous. At C = A+B, (A+B-C)=0, area = A*B, consistent.
So we can unify using these formulas.
But we also have degenerate rectangles when A=0 or B=0. And also when A=0 and B>0, probability = min(C/B,1). Similarly B=0, A>0 => min(C/A, 1). Both A=0 and B=0 => probability=1 if C>=0 (which is always true given C>=0). But if A=0, B=0, we can treat as probability 1.
But also when A>0 but B=0: then y=0 always, condition x ≤ C, probability = min(1, C/A). Similarly.
Now for A=0, B>0: as above.
Now careful: The problem states "We choose two real numbers x and y such that 0 ≤ x ≤ A and 0 ≤ y ≤ B." This implies uniform distribution over the rectangle? Typically yes. So sample space area is A*B. But if A=0 or B=0, the "area" is 0, which would make probability undefined under continuous uniform distribution over region of zero area. However we might interpret that the random variables are each uniformly distributed over their intervals independently. If one interval is of zero length (a point), then that variable is deterministic. In that case the joint distribution is a one-dimensional uniform (if the other is non-zero) or a point mass. So we can define probability as the measure of the set of y such that condition holds (since x is fixed) divided by B (if B>0). So the usual notion: x ~ Uniform[0,A], y ~ Uniform[0,B] independent. If A=0, then x is degenerate at 0, and we consider its distribution as a point mass. So probability = P(0 + y ≤ C) = P(y ≤ C). Since y is uniform on [0,B], probability = min(1, C/B). Similarly. So we must handle that.
Thus algorithm:
For each test case:
Input A, B, C (as integers but treat as real numbers). Compute probability p.
If A == 0 and B == 0: p = 1.0 (since x+y=0 <= C because C>=0).
Else if A == 0: // x=0
If B > 0:
p = min(1.0, C / B) // but ensure C is double, division double.
else: // B==0 already covered? Actually A==0 and B==0 handled, so B>0.
// pass
Else if B == 0: // y=0
p = min(1.0, C / A)
Else: // both positive
total = A * B (as double)
if C <= 0: // but C>=0
area = 0.0
else if C >= A + B:
area = total
else:
// Compute area based on cases
if C <= A and C <= B:
area = 0.5 * C * C
elif C > A and C > B:
// both A and B less than C but C < A+B
deficit = A + B - C // positive
area = total - 0.5 * deficit * deficit
else:
// one of A, B is less than or equal to C, and the other is greater than or equal to C? Actually we are in min < C <= max.
// We can compute area as: if A <= B:
// then if C <= A? Already covered; if A < C <= B: area = AC - AA/2
// else (B < C <= A) if B < A: area = BC - BB/2
// So we can check.
if A <= B:
// then min = A, max = B
if C <= A:
// covered earlier
pass
elif C <= B: // A < C <= B
area = A*C - 0.5 * A * A
else:
// C > B and C < A+B? Actually this is covered by the previous case? Wait if A<=B then max = B, so if C > B then we are in case3 if also C < A+B, else if C>=A+B covered. So here C > B, but we already covered case3 (C > max). So the else branch should only be when C is between min and max. So we need to differentiate.
else: // B < A
similarly.
So to avoid confusion, we can compute area using integration formula that works for all:
x1 = max(0.0, C - B)
x2 = min(A, C)
if x2 <= x1:
area = (if C > 0? Actually if x2 <= x1, then the region is either full rectangle or empty? Let's analyze.
If C >= A+B: handled earlier.
If C <= 0: handled.
In the intermediate, x1 and x2:
x1 = max(0, C-B).
x2 = min(A, C).
For C between 0 and A+B, we will have x2 > x1? Let's verify:
- If C <= min(A,B): then C <= A and C <= B, so C-B <= 0 => x1=0; x2=C; So x2 = C > 0 = x1, good.
- If min(A,B) < C <= max(A,B): suppose A <= B and A < C <= B. Then C-B <= 0? Since C <= B, C-B <=0 => x1=0. x2 = min(A, C) = A. So x2 = A > 0 = x1, good.
- If max(A,B) < C < A+B: suppose A <= B, then C > B, so C-B > 0, so x1 = C-B. x2 = min(A, C) = A (since A ≤ B < C => A < C). So we need to check if A > C-B? Since C < A+B => C-B < A, so x1 = C-B < A = x2. So x2 > x1. So always x2 > x1 for 0 < C < A+B. At C = A+B, x1 = (A+B)-B = A, x2 = min(A, A+B) = A, so x2 = x1, then area would be? Actually at C = A+B, region is full rectangle; but we handled separately with C >= A+B. So it's okay.
So we can compute area = B * max(0, x1) + integral_{x=x1}^{x2} (C - x) dx.
Wait careful: Actually first interval is from 0 to x1, but if x1 > 0, then for x in [0, x1], y_max = B. But if x1 = 0, that interval length 0. So area = B * x1 + ∫_{x1}^{x2} (C - x) dx.
Check: For x from 0 to x1 (if x1>0) we have full B. But is it correct if x1 may be > A? But x1 is defined as max(0, C-B). However, if C-B > A, then x1 > A, but then C >= A+B? Because if C-B > A => C > A+B. That case we already covered (full rectangle). So in the range where C < A+B, we have x1 ≤ A? Actually if C < A+B then C-B < A, so x1 = C-B < A; if C-B could be negative then x1=0, so x1 in [0, A). So okay.
Similarly, x2 = min(A, C) and since C < A+B, if C > A then x2 = A, else x2 = C. So x2 ∈ (0, A] and x2 > x1.
So we can compute:
if x2 > x1:
area = B * x1 + ∫_{x=x1}^{x2} (C - x) dx
else:
area = total (if C>= A+B) or 0 (if C <=0) but we have handled that.
Now compute ∫_{x1}^{x2} (C - x) dx = C*(x2 - x1) - (x2^2 - x1^2)/2.
So area = B*x1 + C*(x2 - x1) - 0.5*(x2^2 - x1^2).
Simplify? Could be used directly.
This formula should work for all cases where 0 < C < A+B, including the subcases. Because when C ≤ B, x1=0, then area = 0 + C*x2 - 0.5*x2^2. And x2 = min(A,C). If C ≤ A then x2=C => area = C^2 - 0.5*C^2 = 0.5*C^2. If C > A (but still ≤ B), then x2 = A, so area = C*A - 0.5*A^2. Works.
If C > B, then x1 = C-B >0, x2 = min(A,C). If also C ≤ A (i.e., B < C ≤ A) then x2 = C? Actually if C ≤ A then x2=C, but if C > B and C ≤ A, then we have A >= C > B. Then x2 = C, x1 = C-B. Then area = B*(C-B) + C*(C - (C-B)) - 0.5*(C^2 - (C-B)^2) = B(C-B) + C*B - 0.5*(C^2 - (C^2 - 2BC + B^2)) = B(C-B) + C*B - 0.5*(2BC - B^2) = B(C-B) + BC - (BC - 0.5*B^2) = B(C-B) + BC - BC + 0.5*B^2 = B(C-B) + 0.5*B^2 = BC - B^2 + 0.5*B^2 = BC - 0.5*B^2. Which matches the formula for case when B ≤ C ≤ A? Actually that is when min = B? That's the other case: if B ≤ A and C between B and A, then area = B*C - B^2/2. Indeed matches.
If C > B and C > A (i.e., C > max(A,B)), then x1 = C-B, x2 = A. Then area = B*(C-B) + C*(A - (C-B)) - 0.5*(A^2 - (C-B)^2) = B(C-B) + C(A - C + B) - 0.5*(A^2 - (C-B)^2) = B(C-B) + C(A - C + B) - 0.5*A^2 + 0.5*(C-B)^2. Expand: B(C-B)=BC - B^2. Then C(A - C + B)= C A - C^2 + B C. Sum = (BC - B^2) + (CA - C^2 + BC) = CA - C^2 + 2BC - B^2. Then add -0.5A^2 + 0.5(C-B)^2 = -0.5A^2 + 0.5(C^2 - 2BC + B^2). So total = CA - C^2 + 2BC - B^2 - 0.5A^2 + 0.5C^2 - BC + 0.5B^2 = CA - 0.5C^2 + BC - 0.5B^2 - 0.5A^2? Let's combine correctly: - C^2 + 0.5C^2 = -0.5C^2. 2BC - BC = BC. - B^2 + 0.5B^2 = -0.5B^2. So we have CA + BC - 0.5C^2 - 0.5B^2 - 0.5A^2? But note there is also a term? Wait we have CA (from C A) and BC (from leftover) and -0.5A^2. So area = CA + BC - 0.5A^2 - 0.5B^2 - 0.5C^2? That seems off. Let's do step by step.
Better to trust the known formula: area = AB - (A+B-C)^2/2. But our integration should yield that.
Let's test with numeric example: A=4, B=10, C=11 (from sample). Then A+B=14, C=11, deficit=3, area = 40 - 9/2 = 40 - 4.5 = 35.5. Our integration: x1 = max(0, C-B)=max(0,1)=1; x2 = min(A, C)=min(4,11)=4. area = Bx1 + ∫_{1}^{4} (11-x)dx = 101 + [11x - x^2/2]_{1}^{4} =10 + (44 - 8) - (11 - 0.5) =10 + 36 - 10.5 =35.5. Good.
Thus the integration method works. So we can use it for the non-degenerate case.
But we must be careful with floating point precision? Input up to 1e9 and C up to 1e18, so products like AB can be up to 1e18, which fits in double with 53-bit mantissa (~9e15 precise integer? Actually double can exactly represent integers up to 2^53 ~9e15. 1e18 > 9e15, so there may be loss of integer precision for large values. But we need absolute error <1e-6, so relative error could be small. However if we compute area using double, products like AB for A=1e9, B=1e9 gives 1e18, which is > 2^53, so not exactly representable. That could cause rounding errors but final probability might be off by more than 1e-6? Let's check: If A=1e9, B=1e9, total area = 1e18. Double's mantissa is 53 bits, so integers up to 9e15 are exactly representable. 1e18 is 10^18, which is about 2^60 (since 2^10 ≈1024, 2^60 ≈1.15e18). So the spacing between representable numbers around 1e18 is about 2^(60-53)=2^7=128. So the absolute error could be up to ~128. That might be too large because we need probability absolute error <1e-6, but if we divide by total area 1e18, the error in area is maybe up to 128, then error in probability = 128/1e18 = 1.28e-16, which is far below 1e-6. So it's okay. However if we compute area = AB - (deficit)^2/2, deficit may be up to about 2e9? Actually deficit = A+B-C, could be as small as 0 or as large as about 2e9 (if C small). But when A and B are large, the area is large, and we subtract a relatively small quantity (the triangle). The subtraction may cause cancellation? For instance, if C is just slightly less than A+B, deficit is small, area = AB - small. Then AB is huge (~1e18) and small ~ (deficit^2)/2, deficit maybe up to 2e9 => deficit^2 up to 4e18? Actually deficit can be up to A+B (if C=0), but C could be 0, deficit = A+B up to 2e9, square = 4e18, which is larger than AB? But if C=0, then area=0. But we would compute AB - (A+B)^2/2. For A=B=1e9, A+B=2e9, (A+B)^2/2 = (4e18)/2 = 2e18. But AB = 1e18. Then AB - (A+B)^2/2 = 1e18 - 2e18 = -1e18, negative. So that formula fails for C small because we are not in case C > max(A,B). So we need to use proper case branching. So we will handle accordingly to avoid catastrophic cancellation.
The integration formula with x1 and x2 also involves products like Bx1, and x2^2 - x1^2, etc. But these products might be large but will be subtracted, could cause cancellation? For example, in the case where C is small (0 < C ≤ min(A,B)), x1=0, x2=C, area = CC/2. That's fine.
In case where C is just above max(A,B) but less than A+B, we might use area = AB - (A+B-C)^2/2. That subtraction might cause cancellation if AB and the triangle area are close. But they are close only when deficit is small, i.e., triangle area is small, so AB - small: no cancellation. The two numbers are not close relatively? Actually AB is huge (~1e18) and (deficit^2)/2 is small (if deficit is small, e.g., 1, then (1^2)/2=0.5). So subtraction is fine. The cancellation could occur if deficit is large, but then we are not in that case because that formula is only used when C > max(A,B). If C is just > max(A,B), deficit = A+B-C could be up to? If A and B are 1e9 each, max = 1e9. If C is just above 1e9, then deficit ≈ 1e9? Actually if C=1e9+1, then deficit = 2e9 - (1e9+1) ≈ 1e9-1, which is still about 1e9, and (deficit^2)/2 ≈ (1e18)/2 = 5e17, and AB = 1e18, so subtracting yields about 5e17. That's not catastrophic because both are large but difference is also large. However there could be loss of precision if the two numbers are very close? They are not extremely close relative to their magnitude: the difference is about half of AB, so relative error is not magnified. So safe.
The only potential for catastrophic cancellation is if we use the formula area = AB - (A+B-C)^2/2 when deficit is close to sqrt(2*AB)? But that's the case when area is near zero? Actually area can't be negative. The formula is used only when C > max(A,B). In that range, area is at least? Minimum when C = max(A,B) gives area = AB - (A+B-max)^2/2. For A=B, C = A => area = A^2 - (2A-A)^2/2 = A^2 - A^2/2 = A^2/2. That's still large relative to AB. So no near zero area in that branch. The near zero area occurs only when C is small, which uses C^2/2, no subtraction.
Thus double precision is fine.
But we also have to handle large number of test cases (1e5) so code should be efficient, O(1) per test.
Now we also need to consider degenerate cases where A=0 or B=0. But note: when A=0 and B>0, we said probability = min(1, C/B). But is that correct if C is huge? Yes, because y is uniform in [0,B], P(y ≤ C) = 1 if C >= B, else C/B. However note that if C=0, then probability = 0. Good.
But also we need to consider if A=0 and B=0: probability = 1.
Similarly B=0 and A>0: probability = min(1, C/A).
Edge cases: A=0, B=0, C=0: probability=1.
But also what about A=0, B>0, C negative? Not possible as C>=0.
Now we need to output probability with enough precision.
Implementation steps per test case:
Read A, B, C as long long (since C up to 1e18, A,B up to 1e9, but product AB up to 1e18 fits in signed 64-bit? 1e91e9 = 1e18, which is just at the limit of 2^63-1 ~9.22e18, so safe. However if we use signed long long (int64), 1e18 is fine. But to be safe, we can use unsigned long long, but using signed is okay as all non-negative. However, we might compute A+B-C, which could be negative if C > A+B. But we handle separate cases, so we won't compute negative under that branch. But we need to compute deficit = A+B-C only when C < A+B. So we can compute as long double maybe for extra precision? But we can stick to double. But careful with integer to double conversion: use double for all.
But for the cases where we compute area = AC - 0.5A*A (or similar) we need to be careful with possible large numbers, but double fine.
Given T up to 1e5, we can do double operations, that's fine.
Now we need to compute probability = area / (AB) for non-degenerate case. But if AB = 0 (when one is zero), we already handle separately.
Double-check the integration formula: area = B * x1 + (C*(x2 - x1) - 0.5*(x2x2 - x1x1)). Actually integrate (C - x) dx from x1 to x2 = C*(x2 - x1) - (x2^2 - x1^2)/2.
So area = B * x1 + C*(x2 - x1) - 0.5*(x2^2 - x1^2).
But need to ensure that we are not double counting if x1 > A? As argued earlier, we only use when C < A+B. So that is safe.
Alternative: Use the piecewise formulas directly for simplicity and avoid computing x1,x2 and squares. Let's examine which is simpler.
Piecewise formulas:
if C <= 0: p=0 (but C>=0, so only C=0, but careful when A=0 and B=0 gives p=1 else maybe 0? Actually if both A>0 and B>0, C=0 gives p=0. So we can treat as area=0.
else if C >= A+B: p=1.
else:
if C <= A and C <= B: area = 0.5 * C * C
elif C <= A and C > B: // Actually C <= A but > B? That implies B < C <= A. So area = BC - 0.5BB
elif C <= B and C > A: // A < C <= B: area = AC - 0.5AA
else: // C > A and C > B: area = AB - 0.5(A+B-C)*(A+B-C)
That is clear. And we need to be careful about which case is which. This can be coded with conditionals.
But note: C <= A and C > B is possible only if B < A? Actually if B < A, then B could be less than A. So we need to check relationships.
Better: Let minAB = min(A,B), maxAB = max(A,B).
If C <= minAB: area = 0.5CC
elif C <= maxAB: // i.e., minAB < C <= maxAB
if A <= B: // then min=A, max=B. For C between A and B: area = AC - 0.5AA
else: // B < A. For C between B and A: area = BC - 0.5BB
else: // C > maxAB (and C < A+B)
area = AB - 0.5(A+B-C)*(A+B-C)
But careful: When A <= B, and C > A but C <= B, we are in the elif branch with condition C <= maxAB and > minAB, so we apply that. When C > B (i.e., > maxAB), we go to else.
Similarly for B < A.
That works.
But we must ensure that if A == B, then min = max, and the elif branch (C <= maxAB) and > minAB cannot happen because > minAB would be > maxAB, so no. So when A == B, only cases: C <= A => triangle, else if C > A and C < 2A => else branch (the deficit). This matches.
Now what about when A=0 or B=0? Those are degenerate and handled separately.
Thus algorithm:
For each test case:
Read A, B, C as double? Actually as long long to avoid precision issues, but we'll convert to double for computations.
But we must be careful: A and B are integers up to 1e9, C up to 1e18, so we can store them in unsigned long long, but for double conversion it's fine.
Pseudo:
double solve_one(long long A, long long B, long long C) {
if (A == 0 && B == 0) {
return 1.0;
}
if (A == 0) {
if (B > 0) {
if (C >= B) return 1.0;
else return (double)C / B;
}
// actually B == 0 covered above, so not needed.
}
if (B == 0) {
if (A > 0) {
if (C >= A) return 1.0;
else return (double)C / A;
}
}
// Now both A>0 and B>0
double a = A, b = B, c = C;
double total = a * b;
if (c <= 0) return 0.0; // though c>=0 so only c=0. But if A>0 and B>0, area 0.
if (c >= a + b) return 1.0;
double area;
if (c <= a && c <= b) {
area = 0.5 * c * c;
} else if (c <= a && c > b) {
// This can only happen if b < a and c > b and c <= a
area = b * c - 0.5 * b * b;
} else if (c <= b && c > a) {
// a < b and c > a and c <= b
area = a * c - 0.5 * a * a;
} else {
// c > a and c > b, and c < a+b
double deficit = a + b - c; // positive
area = total - 0.5 * deficit * deficit;
}
return area / total;
}
But we must ensure that the conditions for the middle two cases are correctly triggered. Since we used if-else cascade, we can rely on ordering:
if (c <= a && c <= b) // both less or equal.
else if (c <= a) // then we know c <= a but not (c <= b), so c > b. So this covers case c <= a and c > b.
else if (c <= b) // then c > a (since previous failed) and c <= b. So covers c > a and c <= b.
else // c > a and c > b.
But careful: The condition c <= a could be true even if c <= b is also true, but we already handled case where both are true in first if. So else if (c <= a) means c <= a and c > b (because if c <= b would have been caught by first if? Actually if both c <= a and c <= b, first if catches. So after first if fails, we know that not (c <= a and c <= b). It is possible that c <= a but c > b, or c <= b but c > a, or both false. So else if (c <= a) will be true when c <= a and not (c <= a && c <= b) i.e., c <= a but c > b. Similarly else if (c <= b) will be true when c <= b and c > a (since if c <= a it would have been caught earlier). So this logic is correct.
But we should be cautious about floating point comparisons when A, B, C are integers. Since we convert to double, equality is exact for integers up to 2^53. A, B up to 1e9 (fits exactly). C up to 1e18, which may not be exactly representable. But we only compare with sums and products that might also not be exact. However, absolute error tolerance 1e-6 allows small errors. But to be safe, we might want to keep comparisons on integers where possible, using integer arithmetic for conditions to avoid rounding issues on boundaries. Because if C is exactly equal to A+B, we want to return 1 exactly (or close). If we compute using double, due to conversion maybe c == a + b might not hold exactly if a+b is large and not exactly representable? For example, a=1e9, b=1e9, a+b=2e9, which is within 2^53? 2e9 < 9e15, so it's exactly representable. c = 2e9 exactly if C=2000000000, but C can be up to 1e18. For C=1e18, that is not exactly representable? 1e18 is 1000000000000000000. Double representation of 1e18 has some rounding; but if C is given as integer up to 1e18, converting to double will produce the closest representable double. a+b might also be up to 2e9 (exact). So comparing c >= a+b: if C is exactly A+B, the double values may not be exactly equal due to rounding of C? Actually if C = 2000000000 (2e9) exactly, that is within 2^53, so double can represent it exactly. For larger C up to 1e18, many integers are not exactly representable. But if C is exactly equal to A+B (which is at most 2e9), then C is small (<=2e9) and exactly representable. So fine. So boundary conditions involving equality with C and A or B? A,B up to 1e9, exactly representable. C can be up to 1e18, but if C is exactly equal to A or B (which are ≤1e9), that means C ≤1e9, exactly representable. So equality checks with A and B are safe. The only potential large C equality is with A+B which is ≤2e9, safe. So using double for conditions is safe as long as we convert integer to double; the conversion for integers up to 2^53 is exact. So we can rely on exact comparisons for these cases.
But the else branch uses deficit = a+b-c; if c is close to a+b but slightly off due to rounding? But we only go into else branch when c < a+b (since we have prior if c >= a+b returns 1). So if c is just less than a+b due to rounding but actually equal (integer wise), the comparison c >= a+b would be false because c is a double representing the integer value exactly? Wait, if C = A+B exactly, then c is exactly a+b (both exact). So c >= a+b true. So it will return 1, no problem. If C is slightly less than A+B but due to rounding? Not, because C integer, a+b integer, both representable exactly (if less than 2^53). So no issues.
But we also need to compute area = total - 0.5 * deficit^2. total = ab. For a,b up to 1e9, ab up to 1e18. That's near the limit of exact integer representation? 1e18 > 9e15, so not exactly representable. But the subtraction may be sensitive? But as argued earlier, relative error is small. Also, if deficit is also large? deficit can be up to a+b (2e9) which is exactly representable, its square up to 4e18, which is also not exactly representable; but we are subtracting from total (which is also large). The difference might be large, but the error relative to total is small. However, to ensure high precision, we could use long double (80-bit) on many compilers for extra precision. But not necessary.
Now we must handle the case where A>0, B>0, but total area = A*B. However, if A and B are both 0, already handled.
Now let's test sample:
Input:
4 10 3 => A=4,B=10,C=3.
c <= a? 3 <= 4 true; c <= b? 3 <= 10 true; so first case: area = 0.5*9=4.5. total=40. p=0.1125 matches.
Second: 4 10 11. a=4,b=10,c=11. c <= a? false; c <= b? 11 <=10 false; So else? Actually c > a and c > b? Both true, and c < a+b=14 so else case: deficit = 4+10-11=3; area=40-0.5*9=40-4.5=35.5; p=35.5/40=0.8875 matches.
Now test some edge cases:
A=0, B=10, C=5: p=0.5. Our code: A==0, B>0, C/B=0.5 -> correct.
A=0, B=10, C=15: p=1.
A=5, B=0, C=2: p=2/5=0.4.
A=5, B=0, C=10: p=1.
A=0, B=0: p=1.
A=10, B=10, C=0: both >0, c<=0? c=0. Our code: if c <= 0 return 0.0? Actually c <= 0 is true, so area =0. So p=0. But is that correct? x and y are uniform on [0,10] each, probability that x+y ≤ 0 implies x=y=0, which is a single point, probability 0. So correct.
A=10, B=10, C=10: min =10, c <= a and c <= b true? 10 <=10 true and 10 <=10 true -> first case: area = 0.5 * 10*10 = 50. total=100 => p=0.5. Indeed line from (0,10) to (10,0) exactly goes through the corner. So half the square. So correct.
A=10, B=10, C=15: c <= a? 15 <=10 false; c <= b? false; So else? Not yet, check else if (c <= a) false; else if (c <= b) false; So else case: c> a and c> b, c < a+b=20 so deficit = 20-15=5, area = 100 - 12.5 = 87.5, p=0.875. That matches: the triangle cut off has legs 5. So area = 100 - 25/2 = 100 - 12.5 = 87.5.
A=10, B=20, C=15: min=10, max=20, c=15: c <= min? 15 <=10 false; So not first. else if (c <= a)? a=10, c<=10? false. else if (c <= b)? b=20, c<=20 true, and we know from earlier that first failed and c<=a failed, so c>a. So area = ac - 0.5aa = 1015 - 0.5*100 = 150 - 50 = 100. total=200, p=0.5. Check geometric: For x from 0 to 10, y_max = min(20, 15-x) = 15-x (since 15-x ≤ 15 ≤20). So area = ∫0^10 (15-x) dx = 150 - 50 =100. Yes.
A=20, B=10, C=15: a=20,b=10; c=15. first? c <= a? true; c <= b? 15 <=10 false; So else if (c <= a) is true (since c<=20). That will execute area = bc - 0.5bb = 1015 - 0.5*100 =150-50=100. total=200, p=0.5. correct.
So logic works.
Now we must also consider case where A>0,B>0 but one of them maybe zero? Already handled.
Now complexity O(T). T up to 1e5, simple.
Potential pitfalls: When C is extremely large but less than A+B? Wait A+B is at most 2e9, so if C is up to 1e18, the only possibilities are either C >= A+B (since A+B ≤ 2e9). So for most large C, probability = 1. But if C is very large, we go to the first if C >= a+b and return 1. So fine.
Now what about C between 0 and A+B? Since A+B max 2e9, C can be up to 2e9. So C fits in 32-bit? Actually up to 2e9, which fits in signed 32-bit? 2e9 < 2^31-1=2.147e9, so it's safe. But note C can be given up to 1e18, but if it's > 2e9, it's automatically >= A+B, so probability 1. So we can also add a check: if C >= A+B, return 1. That's cheap with integer arithmetic (avoid conversion to double). But we need to compute A+B as integer (max 2e9, safe). But we have to handle the case where A or B is zero? If A=0, B>0, we check earlier; but we could also integrate that into generic logic? I'll keep separate handling for simplicity.
Now double-check the degenerate cases:
-
A=0, B>0: probability = min(1, C/B). Since y uniform on [0,B], P(y ≤ C) = 1 if C >= B else C/B. However, what if C is negative? Not possible.
-
B=0, A>0: similarly.
-
A=0, B=0: probability = 1.
Edge: A=0, B>0, C=0: p=0, since y ∈ [0,B] positive probability of y=0? Actually y continuous, probability y=0 is zero, so probability y ≤ 0 is zero (since only y=0 satisfies). So p=0. Our code: C/B = 0/B = 0 => correct.
- A=0, B>0, C=10, B=5: C/B=2 => min(1,2) = 1, correct.
Now if A>0,B>0, but one of them is zero? Not possible.
Now handling of A and B as long long: use double for division.
Now implement.
Potential overflow: In area computation for case b * c - 0.5 * b * b, if b and c are large (b up to 1e9, c up to 2e9), product up to 2e18, which may exceed 2^63-1 if using integer, but we use double, so fine.
Now we also need to ensure we don't compute 0.5 * b * b as b * b / 2. But with double safe.
Now test additional edge cases:
-
A=1000000000, B=1000000000, C=0 => p=0.
-
A=1000000000, B=1000000000, C=1000000000 (C = A = B) => p = 0.5. area = 0.5 * C^2 = 0.5 * 1e18 = 5e17, total = 1e18, p=0.5.
-
A=1000000000, B=1000000000, C=1500000000 (1.5e9) => C > A and > B? Yes, C=1.5e9, A+B=2e9, deficit=5e8, area = 1e18 - 0.5 * (5e8)^2 = 1e18 - 0.5 * 2.5e17 = 1e18 - 1.25e17 = 8.75e17, p=0.875.
-
A=1, B=1000000000, C=500000000. Then min=1, C > A? 500M > 1, C <= B? 500M <= 1e9 true. So area = AC - 0.5AA = 1500M - 0.5*1 = 500M - 0.5. total = 1e9. p ≈ 0.5 - 5e-10? Actually 500M/1e9=0.5, minus 0.5/1e9=5e-10, so ~0.4999999995. Acceptable.
Now, for extremely large C > 2e9, we return 1. But what about degenerate where A=0, B=0, C=0 returns 1; A=0,B=0,C=10^18 returns 1.
Now check A=0, B=10, C=10 exactly: C/B = 1, return 1. correct.
A=0, B=10, C=9.999999? but C is integer input, so C=9 -> p=0.9.
Now ensure that we use double division for probability in degenerate cases.
Now time to code.
But wait: The problem statement says "We choose two real numbers x and y such that 0 ≤ x ≤ A and 0 ≤ y ≤ B." It doesn't explicitly say they are uniformly distributed, but it's implied for probability. Typically in such problems, uniform distribution is assumed.
Now also note that A,B,C are integers, but x,y real. So continuous uniform.
Now implement.
One more subtlety: In the non-degenerate case, total area = A*B. However, if A and B are integers but may be zero? We already handled separately.
Now we might also consider when C is negative (not allowed).
Now code.
I'll write a function compute_prob(A,B,C) returning double.
Pseudo:
double compute_prob(long long A, long long B, long long C) {
if (A == 0 && B == 0) return 1.0;
if (A == 0) {
if (B == 0) return 1.0; // redundant but ok.
if (C >= B) return 1.0;
return (double)C / B;
}
if (B == 0) {
if (C >= A) return 1.0;
return (double)C / A;
}
// both >0
double a = A, b = B, c = C;
double total = a * b;
if (c <= 0) return 0.0; // could be c==0
if (c >= a + b) return 1.0;
double area;
if (c <= a && c <= b) {
area = 0.5 * c * c;
} else if (c <= a) { // c <= a and c > b
area = b * c - 0.5 * b * b;
} else if (c <= b) { // c <= b and c > a
area = a * c - 0.5 * a * a;
} else { // c > a and c > b
double deficit = a + b - c;
area = total - 0.5 * deficit * deficit;
}
return area / total;
}
Testing with sample: ok.
But we need to be careful: In the else if (c <= a) case, we assume that b is the smaller? Actually it doesn't matter: we compute area = bc - 0.5bb. That is correct for the scenario where c > b and c <= a. In that scenario, b is the smaller side? Actually if c <= a and c > b, then b < c <= a, so b is indeed the smaller of A and B? Not necessarily: Could be that a < b? Wait, if c <= a and c > b, then b < c <= a, thus b < a, so b is min. Yes. So area formula uses the min side. So it's consistent: area = min(A,B)C - 0.5 * (min)^2? Actually when C is between min and max, the area formula uses the smaller side. In the scenario where A <= B (so A is min) and C is between A and B, area = AC - 0.5A^2, which uses A (the smaller). In our else if (c <= a) case, we compute using b, but that assumes that b is the smaller side. But can it happen that a is the smaller side yet c <= a and c > b? If a is the smaller, then b is larger. Then condition c <= a and c > b cannot happen because b > a >= c? Actually if a is smaller, then b is larger. For c <= a and c > b, we would need b < c <= a, but b is larger, contradiction. So that case can only happen when b is the smaller. So the condition (c <= a && c > b) implies that b is the smaller. Because if b > a, then c > b > a, but c <= a is false. So indeed, when we are in else if (c <= a), we know b is the smaller. So area = bc - 0.5bb is correct. Similarly, else if (c <= b) implies a is the smaller and we compute area = ac - 0.5aa. So correct.
Now edge: what if a == b? Then first case (c <= a && c <= b) covers c <= a. So else if won't trigger because c <= a will be true but then also c <= b true? Actually if a == b, then c <= a and c > b cannot happen because b = a, so c > b implies c > a, but then c <= a is false. So else if (c <= a) false because condition c <= a true but we must have also that we are not in first case, meaning not (c <= a && c <= b) which is equivalent to not (c <= a) because b = a. Actually if a==b, first case is c <= a. If c <= a, we go to first case. So else if (c <= a) will never be entered when a==b because if c <= a, first case catches, and if c > a, then c <= a false, so else if false. So we go to else (c > a and c > b) when c > a. So it's consistent.
Thus logic is correct.
Now we also need to ensure that we treat the case where C is exactly equal to the smaller side but not larger? That falls in first case if C <= both. So if C equals the smaller but less than the larger (e.g., A=4, B=10, C=4), then C <= A true, C <= B? 4 <= 10 true, so first case, area = 0.5*16=8, total=40 => 0.2. Let's verify geometrically: line from (0,4) to (4,0). That cuts the rectangle at left-bottom triangle of area 8. Indeed, half of the 4x4 square (since the rectangle extends to y=10, but the triangle only covers up to y=4, it's exactly that triangle). So probability 0.2. Good.
Now check when C exactly equals the larger side but greater than smaller? e.g., A=4, B=10, C=10. Then C <= a? 10 <= 4 false; C <= b? 10 <= 10 true; and we are not in first case because not both <=. else if (c <= a) false; else if (c <= b) true, so area = ac - 0.5aa = 410 - 0.5*16 = 40 - 8 = 32, total=40, p=0.8. That matches geometric: For x from 0 to 4, y_max = min(10, 10-x) = 10-x (since 10-x ≤ 10). So area = ∫0^4 (10-x)dx = 40 - 8 = 32. Good.
Now check when C exactly equals A+B: then c >= a+b, we return 1. Correct.
Now what about C between max and A+B: e.g., A=4, B=10, C=12. Then c > a and c > b, deficit = 4+10-12=2, area=40-2=38? Actually 0.5*4=2, so area=38, p=0.95. That matches.
Now one more degenerate: A=0, B=0, C=0 => p=1.
Now implement.
But note: we have to read T up to 1e5, so use fast I/O (scanf/printf) in C++ or equivalent.
Now ensure we use double and output with appropriate precision. Use printf("%.10lf\n", p) or maybe cout with fixed and setprecision.
Now we also should be careful about integer overflow in intermediate integer comparisons if we do them before converting to double. But we are using long long (64-bit). For condition C >= A+B, we can compute as if (C >= A + B) but A+B might overflow 64-bit? A and B up to 1e9 each, sum up to 2e9, well within 64-bit. So safe. But if we use long long for A,B (int64), sum fits. So we could do early integer check before converting to double. But our code uses double for a,b,c; but we could also keep integer logic for comparisons to avoid any subtle rounding. I think it's okay either way.
But note: For the case where A=0 and B>0, we compute C/B. If C and B are integers, converting to double and dividing yields a double. Acceptable.
Now confirm that in the case A>0, B>0, and C=0, we return 0.0. That's fine.
Now also consider A>0, B>0, but one of them may be zero? Already separated.
Now about using condition if (c <= 0) returns 0.0: But if A>0,B>0 and C=0, we get probability 0. However, if A=0,B=0, we already handled and return 1, which is correct because x+y=0. So the ordering is important: We first check A==0 && B==0, then A==0, then B==0. So if A=0,B=0, it's caught and returns 1. So then for A>0,B>0, C=0 returns 0.
Now test: A=0,B=10,C=0: goes to A==0, returns C/B=0 => correct.
Now finalize.
But there is a nuance: When A=0 and B>0, we treat x as fixed at 0, y uniform. However, the probability distribution of (x,y) is degenerate: the joint distribution is not absolutely continuous w.r.t. Lebesgue measure in the plane, but we define probability as P(y ≤ C). That's fine.
Similarly for B=0.
Now if A=0 and B=0, it's a point mass, so probability 1.
Thus code is ready.
Now let's think about potential pitfalls with large numbers and double:
-
In the else if (c <= a) case: area = bc - 0.5b*b. b and c are up to 1e9 and 2e9 respectively, product up to 2e18. Double can represent integers exactly up to 2^53 ≈ 9e15, so 2e18 may lose some low bits. But we then divide by total (also up to 1e18) to get probability. The error in area might be up to maybe a few hundreds, as earlier. So final error ~1e-16. So safe.
-
In the else case: area = total - 0.5deficit^2. total up to 1e18, deficit up to 2e9, deficit^2 up to 4e18, which may exceed 2^53, but we subtract 0.5 that. But note that deficit^2 could be larger than total? Actually if deficit is large, we would not be in the else case because else case requires c > a and c > b and c < a+b. That implies deficit = a+b-c > 0. The maximum deficit in this case is when c is just above max(a,b). If a and b are both large (1e9), max is 1e9, c just above 1e9 gives deficit just under a+b - max = the other side? Actually if a=b=1e9, then max=1e9, c just above 1e9 gives deficit just under 1e9? Actually a+b-c = 2e9 - c. If c = 1e9 + epsilon, deficit ≈ 1e9 - epsilon, so deficit up to 1e9. If a=1e9, b=1e9, then deficit^2 up to 1e18, total = 1e18, so they can be comparable. But note that if deficit is as large as 1e9, then c is close to max, so area = total - 0.5*deficit^2 ~ 1e18 - 0.5e18 = 0.5e18, still huge. The subtraction is between two numbers of similar magnitude, but not catastrophic because the difference is also large (0.5e18). The relative error due to representation of total and deficit^2 may be on the order of (error/0.5e18). If total has an error of about 128, and deficit^2 may have error maybe ~128? Actually deficit^2 (1e18) may have error similar, around 128. So difference error may be ~256, and the result is ~5e17, so relative error ~5e-16. Fine.
So double is fine.
But we must be careful about the case where area is extremely small (close to 0) and total is huge. Example: A=1e9, B=1e9, C=1e-6? Actually C is integer, so at least 0. But C could be 1 (small). Then area = 0.5 * 1^2 = 0.5. Total = 1e18. Probability = 5e-19. Represented as double, that's fine because double can represent numbers as small as ~2e-308. But absolute error maybe 1e-16? The required absolute error is 1e-6, so any probability less than 1e-6 can be output as 0 or small number, and absolute error requirement still allows up to 1e-6. So it's okay even if we output 0.0 for extremely tiny probabilities? But we must ensure we compute area correctly. For C=1, area=0.5, total=1e18, p=5e-19. In double, 5e-19 is representable with about 16 decimal digits of precision, so absolute error is tiny (< 1e-35). So fine.
Now consider A=0, B=1e9, C=1 => p=1e-9. Double can represent that exactly? 1e-9 is representable with some error but absolute error <1e-15? Actually 1e-9 is exact in binary? Not exactly, but absolute error about 1e-25? But fine.
Thus approach is good.
Now implement in C++.
One more nuance: The problem might have multiple test cases, and we need to output each probability.
Now ensure we use long long for A,B,C. In input, A,B up to 1e9, C up to 1e18. So we can use long long (signed 64-bit). I'll use long long.
Now implement:
#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;
double solve(long long A, long long B, long long C) {
if (A == 0 && B == 0) return 1.0;
if (A == 0) {
// B must be >0 here
if (C >= B) return 1.0;
else return (double)C / B;
}
if (B == 0) {
if (C >= A) return 1.0;
else return (double)C / A;
}
// both positive
double a = A, b = B, c = C;
double total = a * b;
if (c <= 0) return 0.0;
if (c >= a + b) return 1.0;
double area;
if (c <= a && c <= b) {
area = 0.5 * c * c;
} else if (c <= a) { // implies c > b
area = b * c - 0.5 * b * b;
} else if (c <= b) { // implies c > a
area = a * c - 0.5 * a * a;
} else {
double deficit = a + b - c;
area = total - 0.5 * deficit * deficit;
}
return area / total;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
long long A, B, C;
scanf("%lld %lld %lld", &A, &B, &C);
double ans = solve(A, B, C);
printf("%.10lf\n", ans);
}
return 0;
}
Test with sample: should output 0.1125 and 0.8875 with .10 maybe more digits but okay.
Now note: In the sample, they output 0.1125 and 0.8875 which have 4 decimal digits. So our output with 10 digits will be fine.
Now ensure the logic for A=0 and B=0: returns 1.0. Good.
Now consider A=0, B=0, C=0: prints 1.0.
Now check A=0, B=10, C=5: prints 0.5.
Now A=5, B=0, C=2: prints 0.4.
Now A=5, B=5, C=0: prints 0.0.
Now A=5, B=5, C=10: prints? c=10, a+b=10, so c >= a+b, returns 1.0. Good.
Now A=5, B=5, C=7: a=5,b=5,c=7. c <= a? false; c <= b? false; else if false; else if false; else -> deficit = 10-7=3; area=25-4.5=20.5; p=20.5/25=0.82.
Now A=0, B=10, C=10: returns 1.0.
Now A=10, B=0, C=10: returns 1.0.
Now A=0, B=10, C=0: returns 0.0.
Now any other tricky cases?
Consider A=0, B=10, C=-1? Not allowed.
Now consider A=0, B=0, C=100: returns 1.0 (since always true). Good.
Now confirm that for A>0, B>0, and C very large (>2e9), we return 1.0. This works because we first check if (c >= a + b). Since a+b is at most 2e9, and c could be 1e18, it's true.
Now confirm that the code does not mistakenly treat A=0, B>0 as total area 0 and division by zero. We handle separately.
Now note: In our code, for A==0 and B>0, we return C/B. But is it correct when C is also 0? Yes.
Now also if A==0, B==0, we return 1.0. That's fine.
Now just to be thorough: Could there be case where A==0, B==0 but C is huge? We return 1.
Now about performance: T up to 1e5, each test constant operations, fine.
Now I think this is correct.
But wait: There is nuance: When A>0 and B>0, and C=0, we return 0.0. That is fine because area is 0.
Now I'll test with some random values with a Python script to verify against integration formula. But since not needed, I'm confident.
Now one more subtle: The problem says "We choose two real numbers x and y such that 0 ≤ x ≤ A and 0 ≤ y ≤ B." This could be interpreted as: x and y are chosen uniformly at random? Usually yes. But what if A or B is zero? The selection of a real number from an interval of length zero is degenerate: there's only one possible value. So probability is defined accordingly.
Thus solution stands.
But hold on: There is a nuance: If A=0 and B>0, we treat x as always 0. But is it correct to treat it as a uniform selection? There's no uniform distribution on a single point, but we can consider it as a deterministic value. The joint distribution is not absolutely continuous, but still we can compute probability of event as the probability that y ≤ C, because x is fixed. That seems reasonable.
Alternatively, one could argue that if A=0, the rectangle degenerates to a line segment of length B, but its area is 0, so the probability of any event in the plane would be undefined if we treat uniform over area. However, the usual interpretation in such problems when picking x from [0,A] and y from [0,B] independently, even if the interval is of zero length, it's still a valid distribution (degenerate). So we'll go with that.
Now check constraints: A,B can be zero.
Now produce final answer.
But the problem statement says "probability that x+y≤C". So we need to output probability. Our approach matches sample.
Now check sample: they gave 0.1125 and 0.8875, which are 9/80? Actually 4.5/40 = 0.1125, 35.5/40 = 0.8875. Yes.
Now I'm satisfied.
Edge Cases:
-
A=0, B=0, C=0 => 1.0.
-
A=0, B=5, C=5 => 1.0.
-
A=0, B=5, C=4 => 0.8.
-
A=5, B=0, C=6 => 1.0.
-
A=5, B=0, C=3 => 0.6.
-
A=5, B=5, C=1 => area = 0.5, p=0.5/25=0.02.
Now what about A=5, B=5, C=1.5? But C is integer. So not needed.
Now should we consider the case where C is not integer? Input says integers, so fine.
Now I'll produce final answer.
But before finalizing, let me consider if there's any scenario where the above piecewise conditions could be misordered due to floating point rounding with c exactly equal to a or b. Since a and b are exact integers (converted to double), c is integer exact for values up to 2^53. For c up to 1e18, if c > 2^53, it may not be exact. But then c > a and c > b obviously because a,b <= 1e9. So it will be in the else case? Actually if c > 2^53, it's definitely > a+b (since a+b <=2e9 << 2^53) unless c is exactly some large number but still > a+b, so we return 1.0 from the earlier c >= a+b check. So c is not used in the else if branches for large C. So safe.
Thus floating rounding doesn't affect equality comparisons when c may be exactly equal to a or b because those only occur when c is at most max(a,b) ≤ 1e9, which is exactly representable. So safe.
Now everything is fine.
Now think about potential issues with integer overflow in condition (c >= a + b) if we compute a+b in double? But a+b is exact up to 2e9, so fine. Could also do integer check: if (C >= A + B) return 1.0; before converting to double. That might be slightly safer. I'll incorporate integer checks where possible.
Refine: after handling degenerate cases, we can do:
if (C >= A + B) return 1.0;
because A+B <= 2e9, and C is integer up to 1e18, but if C >= A+B (integer) then probability 1.
Similarly if (C <= 0) return 0.0; but C>=0, so only C==0, but careful: if A>0,B>0 and C==0, probability 0, correct. So we can do integer check: if (C == 0) return 0.0; But also if C < 0? Not possible.
So we can keep double but it's fine.
Now we also need to compute total = (double)A * B, etc.
But for the piecewise conditions, we need to compare C with A and B. Since they are integers, we can compare as integers. That would avoid any conversion issues. And then use double for area computations.
So better: use integer comparisons for conditions that involve equality with A, B, and check ranges. Use double for computations of area and total.
Thus:
if (A == 0 && B == 0) return 1.0;
if (A == 0) {
if (C >= B) return 1.0;
else return (double)C / B;
}
if (B == 0) {
if (C >= A) return 1.0;
else return (double)C / A;
}
// both >0
if (C <= 0) return 0.0;
if (C >= A + B) return 1.0;
double a = A, b = B, c = C;
double total = a * b;
double area;
if (C <= A && C <= B) {
area = 0.5 * C * C; // C is integer but could be up to 1e9, fine.
} else if (C <= A) { // implies C > B
area = B * C - 0.5 * B * B;
} else if (C <= B) { // implies C > A
area = A * C - 0.5 * A * A;
} else { // C > A and C > B
double deficit = (A + B - C); // positive
area = total - 0.5 * deficit * deficit;
}
return area / total;
But note: In the first branch, we used C as integer for area. That's fine. For area = 0.5 * C * C, if C is large (up to 1e9) product is up to 1e18, may not be exact in double, but error small.
In other branches, we use B * C, etc. We can convert to double: double area = (double)B * C - 0.5 * B * B; But B and C up to 1e9 and 2e9, product up to 2e18. double may round, but ok.
Thus final code.
Now about the output format: They likely accept any format with enough precision. Using printf("%.10lf\n", ans) is fine.
Now I'll produce final answer.
Before final, let's test a few manually with code:
Case: 1 1 0 -> A=1,B=1,C=0 => both >0, C <=0? C==0 -> return 0.0. Good.
Case: 1 1 1 -> C <= A and <= B? 1 <=1 true, area=0.5*1=0.5, total=1, p=0.5. Good.
Case: 1 1 2 -> C >= A+B? 2 >=2 -> return 1.0. Good.
Case: 1 2 1 -> A=1,B=2,C=1: C <= A? 1<=1 true, and C <= B? 1<=2 true, so first branch area=0.5, total=2, p=0.25. Check: rectangle 1x2, line x+y=1: triangle area 0.5, probability 0.25. Good.
Case: 1 2 2 -> C <= A? 2 <=1 false; C <= B? 2<=2 true; so else if (C <= B) true, area = AC - 0.5AA = 12 - 0.5*1 = 2 - 0.5 = 1.5, total=2, p=0.75. Check: For x from 0 to 1, y_max = min(2, 2-x) = 2-x (since 2-x ≤2), area = ∫0^1 (2-x) dx = 2 - 0.5 = 1.5. So 0.75.
Case: 2 1 2 -> A=2,B=1,C=2: C <= A? 2<=2 true, C <= B? 2<=1 false, so else if (C <= A) true, area = BC - 0.5BB = 12 - 0.5*1 = 2 - 0.5 = 1.5, total=2, p=0.75. Symmetric.
Case: 2 1 3 -> C=3: C <= A? 3<=2 false; C <= B? 3<=1 false; So else (C > A and C > B) and C < A+B=3? Actually A+B=3, C=3 -> should be full area, but we check if C >= A+B earlier: C >= A+B? 3 >= 3 true -> return 1.0. Good.
Case: 2 1 2.5? Not integer.
Now edge with zero:
Case: 0 0 5 -> returns 1.
Case: 0 5 0 -> returns 0.
Case: 5 0 0 -> returns 0.
Case: 0 5 5 -> returns 1.
Case: 0 5 6 -> returns 1.
Case: 0 5 4 -> returns 0.8.
Works.
Now think about scenario where A>0, B>0 but C is huge > A+B: Already handled.
Now think about scenario where A>0, B>0, and C is between 0 and A+B but C is extremely large? Not possible because A+B ≤ 2e9.
Now time to produce final answer.
But wait: The input constraints: A,B ≤ 10^9, C ≤ 10^18. So C could be large, but as argued, probability is 1.
Now there is a possibility: A and B are huge (like 1e9) and C is just below A+B (like 2e9 - 1). Then deficit = 1, area = total - 0.5, total = 1e18, probability = 1 - 0.5/total ≈ 1 - 5e-19. Outputting 1.0 may be within absolute error? 0.5/total = 5e-19, which is less than 1e-6, so 1.0 is acceptable. But if we compute exactly, it might be slightly less than 1. However, absolute error of outputting 1.0 would be about 5e-19, which is far below 1e-6, so it's acceptable to output 1.0. But our code will compute area/total = (total - 0.5)/total = 1 - 0.5/total, and when printed with enough digits, it will show as 1.0 maybe? Actually if total=1e18, 0.5/total = 5e-19, double representation of 1 - 5e-19 might be exactly 1.0 because double cannot represent that difference; the nearest double to 1 - 5e-19 is 1.0, because the epsilon of double around 1 is about 2.2e-16. So it will be exactly 1.0. So our code will output 1.0, which is fine.
Similarly for very small probabilities near 0, double may round to 0.0, which is also within absolute error.
Thus no problem.
Now about potential integer overflow when computing A+B-C for deficit: if we compute as (A + B - C) but C may be larger than A+B? But we only compute deficit in the else case, which is only when we know C < A+B (and > A and > B). So A+B - C is positive, and since A+B <= 2e9, C < A+B, difference is small (up to 2e9). So safe.
Now we should also consider if A and B are zero but C negative? Not allowed.
Now finalize.
But hold on: The problem says "We choose two real numbers x and y such that 0 ≤ x ≤ A and 0 ≤ y ≤ B." The selection is uniform? Usually it's implied. But what if they mean "choose any real numbers" meaning all points equally likely? Yes.
Now everything seems fine.
Now I'll write final answer.
But before final, I'd like to double-check the condition order for degenerate cases:
if (A == 0 && B == 0) return 1.0;
if (A == 0) ... if (B == 0) ...
So if A=0, B=0, first if returns, so no issue. If A=0, B>0, we go to second if, compute accordingly, and return. If A>0, B=0, we go to third if. So it's correct.
Now about the case A=0, B=0, C=0: returns 1.0.
Now what about A=0, B=0, C=some positive number: also 1.0.
Now sample provided doesn't include degenerate.
Now we should also consider if A and B are large, but C is very small, like 1. Then area=0.5, total=1e18, probability=5e-19. Double will represent as about 5e-19, which is representable. So output may be 0.0000000000 (if printed with limited digits). But we can print with enough digits to show. The problem only cares about absolute error; 0.0 would be accepted? Because absolute error of 5e-19 is less than 1e-6, so it's fine. But we should output the correct value? They may expect non-zero? However, printing 0.0 would be within error? 0.0 vs 5e-19 absolute error = 5e-19 < 1e-6, so acceptable. But we can output with more digits to be precise. But using printf("%.10lf") will output 0.0000000000, which is fine per error tolerance. But the judge likely checks against expected value, and if expected is very tiny, they may have set an absolute error check. Since our computed value may be non-zero but printing 0.0000000000 could be within tolerance. But is the expected value exactly 0? For C=1, true probability is 0.5 / (A*B). If A=1e9,B=1e9, it's 5e-19. The expected output in such test? Probably they don't have such extreme cases because C is integer and up to 1e18, but A,B also up to 1e9, so probability could be as low as about 0.5 / 1e18 = 5e-19. But they allow absolute error 1e-6, so any answer between -1e-6 and 1e-6 would be considered correct for probability near 0? But if they have test case with such tiny probability, they would need to output something like 0.000000? It's okay. However, to be safe, we can compute using long double for more precision and output with maybe scientific notation? But we can keep as is.
But consider worst-case: A=1e9, B=1e9, C=1 => area=0.5, total=1e18, p=5e-19. Printf with %.10lf prints 0.0000000000. The absolute error compared to true is 5e-19 < 1e-6, so accepted.
Now for p near 1 but not exactly, e.g., C=1999999999 for A=B=1e9? Actually A+B=2000000000, C=1999999999, deficit=1, area = 1e18 - 0.5, p = 1 - 5e-19, printed as 1.0000000000. Acceptable.
So no issue.
Now I'm ready.
But wait: The problem example output shows 0.1125 and 0.8875. They have 4 decimal places. So printing 0.1125 exactly? But if we compute using double, we might get 0.112499999999? But using printf with .10f will show 0.1125000000 maybe due to rounding. So fine.
Now I'll write final solution.
Potential optimization: Use long double for all to avoid any chance of error, but it's fine.
Now final answer code.
Let me produce final solution with explanation.
But before finalize, I'll double-check integration formula for the general case using double. However, I realize a potential subtlety: In the else if (C <= A) branch, we compute area = B * C - 0.5 * B * B. This assumes B is the smaller side. But what if A is the smaller side? As argued, if C <= A and not (C <= B), then B must be less than C? Actually C <= A and not (C <= B) means C > B. Since C <= A, we have B < C <= A, so B < A, so B is the smaller. So it's correct.
But what if A and B are equal? Then if C <= A, then C <= B as well, so first branch catches. So else if not entered.
Thus correct.
Similarly else if (C <= B) implies A < C <= B, so A is smaller.
Now we can implement.
Now additional test: A=5, B=3, C=4. min=3, max=5. C=4: > min (3) and <= max (5). Condition: C <= A? 4 <=5 true; C <= B? 4 <=3 false; so else if (C <= A) true, area = BC - 0.5BB = 34 - 0.5*9 = 12 - 4.5 = 7.5. total=15, p=0.5. Check geometrically: For x from 0 to 5, y_max = min(3, 4-x). For x in [0,1], 4-x >=3? Actually if x <=1, 4-x >=3, so y_max=3. For x in [1,4], 4-x between 0 and 3, y_max = 4-x. For x>4, y_max=0. Compute area: rectangle from x=0 to1: width1, height3 => area=3. Then integral from 1 to4 of (4-x)dx = [4x - x^2/2]1^4 = (16-8) - (4-0.5)=8-3.5=4.5. Total = 7.5. Yes, p=0.5.
Now test A=3, B=5, C=4: symmetry. Condition: C <= A? 4 <=3 false; C <= B? 4 <=5 true; so else if (C <= B) true, area = AC - 0.5AA = 34 - 0.5*9 =12-4.5=7.5. Total=15, p=0.5.
Now test A=3, B=5, C=2: C <= A? 2<=3 true; C <= B? 2<=5 true; first branch area=0.5*4=2; total=15 => p~0.13333. Geometric: triangle legs 2, area=2.
Now test A=3, B=5, C=7: C > A? yes; C > B? 7>5 true; C < A+B=8; else branch: deficit = 8-7=1; area=15-0.5=14.5; p=0.96666. Good.
Now test A=3, B=5, C=8: C >= A+B? 8 >=8 true -> p=1.
Now test A=0, B=5, C=2.5? Not integer.
Now all good.
Now we can finalize.
Now I'll produce final answer. ```cpp
#include
double solve(long long A, long long B, long long C) {
// Degenerate cases: point or line
if (A == 0 && B == 0) return 1.0;
if (A == 0) {
if (C >= B) return 1.0;
return (double)C / B;
}
if (B == 0) {
if (C >= A) return 1.0;
return (double)C / A;
}
// Both A and B are positive
if (C <= 0) return 0.0; // only the origin satisfies, probability 0
if (C >= A + B) return 1.0; // the whole rectangle is included
double total = (double)A * B;
double area;
// Piecewise formulas for the area of {x+y ≤ C} inside the rectangle
if (C <= A && C <= B) {
area = 0.5 * C * C;
} else if (C <= A) { // then necessarily C > B (and B is the smaller side)
area = (double)B * C - 0.5 * B * B;
} else if (C <= B) { // then necessarily C > A (and A is the smaller side)
area = (double)A * C - 0.5 * A * A;
} else { // C > A and C > B, but C < A+B
double deficit = (A + B - C);
area = total - 0.5 * deficit * deficit;
}
return area / total;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
long long A, B, C;
scanf("%lld %lld %lld", &A, &B, &C);
double ans = solve(A, B, C);
printf("%.10lf\n", ans);
}
return 0;
}