We need to compute probability that Jerry's sum > Andrew's sum, given that Andrew wins rounds 1 and 2, and Jerry wins round 3. In each round, Andrew and Jerry draw randomly without replacement from the jar of n distinct numbers. After each round, balls are returned, so each round is independent and identically distributed: each round is a fresh independent draw of two distinct balls from the n, uniformly among all ordered pairs? Let's interpret carefully: "In each round, Andrew and Jerry draw randomly without replacement from a jar containing n balls, each labeled with a distinct positive integer. Without looking, they hand their balls to Harry, who awards the point to the player with the larger number and returns the balls to the jar." They draw without replacement, meaning they take two balls out (one for each? Actually, they each draw a ball from the jar: "Andrew and Jerry draw randomly without replacement from a jar" implies they each take a ball from the jar, without replacement, so they get two distinct balls. Then Harry gives the point to the player with the larger number. Then they return the balls to the jar. So each round is independent: after returning, the jar is full again for the next round. So in each round, the outcome is a random unordered pair of distinct balls assigned to Andrew and Jerry equally likely? However, note that the assignment of which ball goes to which player matters. Since they draw without replacement, the two drawn balls are distinct. But who gets which? It says "Andrew and Jerry draw randomly without replacement from a jar". Typically, that means each picks a ball at random from the jar, and they do so without replacement. So Andrew draws first? Or simultaneously? The order of drawing might matter. Usually, if they draw simultaneously, the process is equivalent to selecting two distinct balls uniformly and then randomly assigning one to Andrew and one to Jerry. Since they are symmetric, the pair (A, J) is equally likely among all ordered pairs with distinct balls. So total number of possible outcomes per round: n * (n-1) equally likely ordered pairs (first ball to Andrew, second to Jerry, but if drawing simultaneously, the probability of each ordered pair is the same). Indeed, if Andrew draws first uniformly from n, then Jerry draws uniformly from the remaining n-1, the ordered pairs (a, j) are equally likely with probability 1/(n(n-1)). Conversely, if we consider unordered pair, the assignment of who gets the larger matters. But the problem says: "they hand their balls to Harry, who awards the point to the player with the larger number". So the outcome of who wins is determined. Given this, we have to compute conditional probability: P(Jerry's total > Andrew's total | Andrew wins round1, Andrew wins round2, Jerry wins round3). All rounds independent.
We have three independent draws, each yields an ordered pair (x_i, y_i) where x_i is Andrew's ball value, y_i is Jerry's ball value, with x_i, y_i distinct numbers from the given set. And we condition on x1 > y1, x2 > y2, and y3 > x3.
Given the conditioning, we need to compute the probability that sum(y_i) > sum(x_i). Since we have three rounds, sum(y) = y1+y2+y3, sum(x)=x1+x2+x3.
We are to compute:
P( y1+y2+y3 > x1+x2+x3 | (x1>y1), (x2>y2), (y3>x3) ) = ?
Since rounds independent, we can consider joint distribution of three independent draws, each uniformly over ordered pairs (a,b) with a!=b.
We need: numerator = probability that (x1>y1, x2>y2, y3>x3, and y1+y2+y3 > x1+x2+x3). Denominator = probability that (x1>y1, x2>y2, y3>x3). Because all draws are independent, denominator = P(x>y)^2 * P(y>x). Since each round is symmetric (except for condition). Note P(x>y) = P(y>x) =? For distinct numbers, because each ordered pair equally likely, the probability that x>y is exactly 1/2? Not necessarily if we consider all ordered pairs, since there are n(n-1) ordered pairs. Among these, half have x>y and half have x<y? Because for each unordered pair {u,v} with u≠v, there are two ordered pairs: (u,v) and (v,u). Exactly one of these has x>y (if u>v then (u,v) has x>y; (v,u) has x<y). So indeed number of ordered pairs with x>y = number with x<y = n(n-1)/2. So P(x>y)=P(x<y)=1/2. However, note that if the numbers are not symmetric? Actually, it's just based on ordering, doesn't depend on actual values. For any set of distinct numbers, exactly half of the ordered pairs have larger first. So indeed each round probability of Andrew win = 1/2, Jerry win = 1/2. So denominator = (1/2)^2 * (1/2) = 1/8. But careful: The event "Jerry wins round 3" means y3 > x3, which also has probability 1/2. So denominator = 1/8.
Thus unconditional probability of the conditioning event is 1/8. So denominator is known.
Now numerator is more complex: we need to compute the probability over three independent draws (each from the full set) that Andrew wins first two, Jerry wins third, and sum(y) > sum(x). Because all draws independent, we can consider triple product.
Let S be the set of numbers. Let N = n. Each round i produce (a_i, b_i) where a_i is Andrew, b_i is Jerry. The probability distribution: uniform over ordered pairs with distinct numbers.
We can define for each round, the joint distribution of (a,b) is symmetric: P(a=x, b=y) = 1/(N(N-1)) for x≠y.
We need to compute:
Num = sum_{ (a1,b1), (a2,b2), (a3,b3) all ordered pairs distinct } [I(a1 > b1) * I(a2 > b2) * I(b3 > a3) * I(b1+b2+b3 > a1+a2+a3)] * (1/(N(N-1)))^3
Denom = (1/2)^3 = 1/8
Thus required probability = Num / (1/8) = 8 * Num
Since Denom is simple, we can compute Num as probability of joint event; result = 8 * Num.
Alternatively, we can compute conditional probability directly by considering distributions conditioned on win/loss per round. But maybe we can compute Num via combinatorial counting or probability expression.
Observation: Because the draws are independent, we can treat each round separately. The condition on wins imposes constraints on relative ordering of a_i and b_i. Also condition on sum comparison involves cross-round sums.
We need to compute probability that sum(b) > sum(a) given that a1 > b1, a2 > b2, b3 > a3.
We can think of each round as drawing an unordered pair {x,y} and then assigning winner. But given win/loss, the assignment is determined: for rounds 1 and 2, the larger number goes to Andrew, smaller to Jerry; for round 3, larger goes to Jerry, smaller to Andrew. So under conditioning, the mapping from unordered pair to values is known: For rounds 1 and 2, if we denote the two numbers drawn as u and v with u > v, then Andrew gets u, Jerry gets v. For round 3, Andrew gets v, Jerry gets u.
So we can reframe: For each round i, an unordered pair of distinct numbers is drawn uniformly from all C(n,2) possible pairs. Then the assignment of who gets which is deterministic based on round: For i=1,2, Andrew gets larger, Jerry smaller; for i=3, Andrew gets smaller, Jerry larger. However, is the probability of each unordered pair conditioned on the win/loss the same as unconditional? Actually, we can directly condition: For rounds 1 and 2, given that Andrew wins, the conditional distribution of the unordered pair {u,v} (with u>v) is uniform over all unordered pairs? Let's check: Unconditionally, each unordered pair {u,v} (u>v) occurs with probability 2/(n(n-1)) because there are two ordered pairs: (u,v) and (v,u). Under the condition that Andrew wins (a>b), only the ordered pair (u,v) (where u>v) is allowed. So conditional on Andrew win, the probability of drawing unordered pair {u,v} (with u>v) is proportional to 1/(n(n-1)) for the specific ordered pair (u,v). Since each unordered pair corresponds to exactly one ordered pair that yields Andrew win: the one where Andrew gets the larger. So the conditional probability for unordered pair {u,v} (u>v) is (1/(n(n-1))) / (1/2) = 2/(n(n-1))? Wait: P(unordered {u,v}) under condition a>b: P(a=u,b=v | a>b) where u>v. Unconditional P(a=u,b=v) = 1/(n(n-1)). Since only pairs with u>v survive, the conditional probability is (1/(n(n-1))) / (1/2) = 2/(n(n-1)). So indeed, conditional distribution for each unordered pair (with u>v) is uniform among all C(n,2) unordered pairs? Because total number of unordered pairs with u>v is C(n,2) and each has the same unconditional probability mass (since each has exactly one ordered pair giving Andrew win). So indeed the conditional distribution over unordered pairs (with u>v) is uniform: each has probability 1/C(n,2) = 2/(n(n-1)). Yes because there are C(n,2) unordered pairs, each contributes 1/(n(n-1)) to the conditional? Let's verify: Denominator for condition is 1/2, and each unordered pair {x,y} with x>y has exactly one ordered pair (x,y) probability 1/(n(n-1)). So the sum over all such unordered pairs is C(n,2) * 1/(n(n-1)) = (n(n-1)/2) * (1/(n(n-1))) = 1/2, correct. So conditional probability for that unordered pair = [1/(n(n-1))] / (1/2) = 2/(n(n-1)). And since C(n,2) = n(n-1)/2, 2/(n(n-1)) = 1/C(n,2). So yes, uniform over all unordered pairs. Similarly, for Jerry win (b>a), conditional on Jerry win, the unordered pair is drawn uniformly from all unordered pairs, and then Andrew gets the smaller, Jerry gets the larger.
Thus, given the win/loss outcomes we have, the conditional distribution for each round (given its win outcome) is: the round's unordered pair (two distinct numbers) is uniformly distributed among all C(n,2) possible pairs, and the assignment to players is deterministic: the winner gets the larger number, loser gets the smaller.
Therefore, the problem reduces to: We have three independent rounds, each round we pick an unordered pair {u_i, v_i} uniformly at random from the set of all pairs of distinct numbers (with uniform probability 1/C(n,2)). For rounds 1 and 2, Andrew receives the larger number (call it A_i = max(u_i,v_i), J_i = min(u_i,v_i)). For round 3, Andrew receives the smaller number (A_3 = min(u_3,v_3), J_3 = max(u_3,v_3)). Then we want the probability that J_1 + J_2 + J_3 > A_1 + A_2 + A_3.
All rounds are independent and identically distributed (the same unconditional distribution of unordered pairs). The conditioning on win outcomes is already absorbed into the assignment.
Thus, we need to compute:
P = Probability that (J_1 + J_2 + J_3 > A_1 + A_2 + A_3) given the described assignment.
We can denote for each round i, we have two numbers: one larger L_i, one smaller S_i. For i=1,2: A_i = L_i, J_i = S_i. For i=3: A_3 = S_3, J_3 = L_3.
Thus the sum comparison becomes:
S_1 + S_2 + L_3 > L_1 + L_2 + S_3
Rearranged:
(L_3 - S_3) > (L_1 - S_1) + (L_2 - S_2)
or equivalently:
(L_3 - S_3) - (L_1 - S_1) - (L_2 - S_2) > 0
Define D_i = L_i - S_i, the difference between the larger and smaller in that round (strictly positive because numbers distinct). Then condition is:
D_3 > D_1 + D_2
Note: For rounds 1 and 2, D_i is positive, and we have A_i = S_i + D_i, J_i = S_i. For round 3, D_3 is positive, A_3 = S_3, J_3 = S_3 + D_3.
So indeed the sum condition is simply: D_3 > D_1 + D_2.
Thus, given that we independently draw three unordered pairs (each uniformly from all pairs), and define D_i as the absolute difference between the two numbers (since L_i > S_i), we need the probability that D_3 > D_1 + D_2.
Is that exact? Check: J_1+J_2+J_3 = S_1+S_2+(S_3+D_3) = (S_1+S_2+S_3)+D_3. A_1+A_2+A_3 = (S_1+D_1)+(S_2+D_2)+S_3 = (S_1+S_2+S_3)+(D_1+D_2). So J_sum > A_sum iff D_3 > D_1+D_2. Yes.
So the condition on the sums depends only on the differences of the drawn pairs, not on the actual small numbers. That's a nice simplification.
Therefore, the required probability = P(D_3 > D_1 + D_2) where D_1, D_2, D_3 are i.i.d. random variables distributed as the positive difference of two distinct numbers chosen uniformly from the set of n distinct values. More precisely, each D_i corresponds to drawing an unordered pair uniformly from C(n,2), then taking the absolute difference.
Given that the assignment of winner determines which player gets the larger/smaller, but D_i is symmetric, so it's independent of the assignment.
Thus, the probability we need = P(D_3 > D_1 + D_2) under i.i.d. draws of D.
But careful: In our conditioning, we have fixed which rounds are Andrew wins and which are Jerry wins. However, the joint distribution of (D_1, D_2, D_3) is indeed i.i.d. because each round's unordered pair is drawn uniformly independent, and D_i is a function of that unordered pair. Since the assignment to Andrew/Jerry given the win outcome is deterministic, but we are conditioning on the win outcomes, we have already incorporated that the assignment is fixed. However, does the condition of win outcomes impose any restriction on the unordered pair? For round 1, the condition is Andrew wins, which is equivalent to that the unordered pair is any pair, but we assign larger to Andrew. That doesn't restrict the set of unordered pairs: any unordered pair can produce an Andrew win if we assign the larger to Andrew (which is the case for the ordered pair that gives Andrew the larger). But given we condition on Andrew winning, we restrict to only those ordered pairs where Andrew's number > Jerry's. But we argued that conditional distribution over unordered pairs given Andrew win is uniform over all unordered pairs (since each unordered pair has exactly one ordered assignment that yields Andrew win). So indeed, given that Andrew wins, the unordered pair distribution is uniform over C(n,2). Similarly for Jerry win. So D_i's distribution under conditioning is the same as unconditional distribution of differences from a uniform unordered pair. So yes, D_1, D_2, D_3 are i.i.d. with distribution: choose two distinct numbers uniformly from the set, take absolute difference.
Thus, we need to compute probability that D3 > D1 + D2, where D_i independent with same distribution determined by the given numbers.
We must compute this probability exactly (as a rational maybe) and output as real.
Given constraints: n up to 2000, numbers up to 5000. So we can precompute all pairwise differences? There are O(n^2) pairs, up to ~4 million which is okay (20002000/2 = 2 million). But careful about memory/time? 2 million is fine. But we need to compute distribution of D. Since numbers are up to 5000, the maximum difference is at most 5000-1 = 4999, but actual maximum difference could be up to maybe 5000-1=4999, but numbers are distinct, range maybe up to 5000. So the differences are integers from 1 to maxDiff (max - min). But note: Not all differences may appear multiple times. We need to compute probability mass function of D. Specifically, let the set of numbers be A = {a_1,...,a_n} sorted ascending. Let total number of unordered pairs = M = n(n-1)/2. For each possible difference d, let count[d] = number of unordered pairs (i<j) such that a_j - a_i = d. Then P(D = d) = count[d] / M.
Now we have i.i.d. D1, D2, D3. Need P(D3 > D1 + D2). Since D_i are independent. Compute:
P = sum_{d1, d2, d3} [d3 > d1 + d2] * p(d1) p(d2) p(d3)
This is a convolution-like sum. We can compute it efficiently using prefix sums or cumulative distributions. Since the maximum difference is up to about 5000, we can create an array of size up to maxD = max possible difference (maybe 5000). Actually numbers up to 5000, but they may not cover full range; max difference = max(a) - min(a). Could be as high as 4999 if min=1, max=5000. So size ~5000, which is fine. So we can compute the distribution p[d] for d = 0 to maxD (d >=1 for pairs, but we can include 0 with 0). Then compute double sum:
P = sum_{d3} p(d3) * sum_{d1, d2: d1+d2 < d3} p(d1) p(d2)
Define for each d3, the inner sum is P(D1 + D2 < d3). But careful: D1 and D2 are i.i.d. So we can compute convolution of p with itself to get distribution of S = D1 + D2. Then P(D1 + D2 < d3) = cumulative distribution of S at d3-1 (since strict inequality d3 > d1+d2). Then P = sum_{d3} p(d3) * P(S < d3).
Thus we need to compute the convolution of the distribution of D (which is over integer values from 1 to maxD). The sum S = D1+D2 can range from 2 to 2*maxD. So we need an array up to maybe 10000. That's fine.
But careful: D1 and D2 are i.i.d., but the distribution p[d] may have gaps. We can compute the convolution via double loops: for each d1, for each d2, add p[d1]p[d2] to s[d1+d2]. Since maxD ~5000, naive double loop is 25 million, which is fine (50005000=25e6). With n up to 2000, but maxD is limited by numbers range, not n. However, if n=2000, numbers up to 5000, maxD may be up to 5000, but worst-case we have ~5000 distinct values? Actually numbers are distinct positive integers ≤ 5000, n ≤ 2000, so the maximum range could be up to 5000 (if min=1, max=5000, but n=2000, not all numbers present). The differences are between any two numbers; d can be as high as max(a)-min(a) ≤ 5000-1=4999. So maxD ~5000.
So O(maxD^2) = 25 million, which is acceptable in Python? Probably yes with optimized loops using numpy? But we can do in plain Python with loops? 25 million multiply-add operations maybe borderline but likely fine with PyPy (since time limit maybe 1-2 sec?). Usually Codeforces problems with n up to 2000 expect O(n^2) solutions, and O(maxD^2) up to 25e6 may be okay in Python with careful implementation using list comprehensions? But we also need to compute count[d] efficiently. We can compute counts of differences by sorting numbers and using two pointers? But naive O(n^2) loops to count differences: for each i<j, compute diff and increment count. That's also O(n^2) = up to 2 million for n=2000, which is fine. So overall complexity: O(n^2) to compute count, then O(maxD^2) to compute convolution. maxD is at most about 5000, but if numbers are sparse, maxD might still be up to 5000 but many d values zero. But the convolution loops over all d1,d2 from 1 to maxD, even if many zeros? We can restrict to only non-zero p's? The number of distinct differences may be O(n^2) but actually there can be up to O(n^2) distinct differences? For n=2000, n^2/2 ~2M possible pairs, but differences are integers bounded by 5000, so many pairs will share same diff. So number of distinct d is at most 5000. So we can just iterate over all d from 1 to maxD, with p[d] maybe zero. The double loop over d1,d2 from 1 to maxD is maxD^2 ~25e6, which is fine.
But we need to be careful about floating point precision? We can compute exact rational probability as fraction? But output as real, we can compute with double precision.
Plan:
- Read n, list a.
- Sort a.
- Compute M = n*(n-1)//2, total number of unordered pairs.
- Determine max possible diff: max_a - min_a. Set Dmax = max_a - min_a (maybe up to 5000). But to be safe, we can compute max_diff = max(a) - min(a). Also note that differences can be up to that, but maybe we need to include all differences up to that.
- Initialize count array of size Dmax+1 (or maybe up to 5000) to zeros.
- For each i from 0 to n-2:
for j from i+1 to n-1:
diff = a[j] - a[i]
count[diff] += 1
This O(n^2) loops, at most 2e6, okay.
- Compute p[d] = count[d] / M for each d.
Alternatively, we can compute distribution of D via frequency of differences.
- Compute convolution S = distribution of D1 + D2: create array conv of size 2*Dmax+1 initialized to 0. For d1 from 1 to Dmax:
if p[d1] > 0:
for d2 from 1 to Dmax:
conv[d1+d2] += p[d1] * p[d2] # double sum
But careful: this will double count? Actually p[d1]*p[d2] includes both (d1,d2) and (d2,d1) separately? Since D1 and D2 are independent, the convolution sum over all (d1,d2) yields distribution of sum. The probability that D1 = d1 and D2 = d2 is p[d1]p[d2]. So summing over all ordered pairs. That is correct. So conv[t] = sum_{d1,d2: d1+d2=t} p[d1] p[d2]. Then the cumulative distribution: F_S(k) = sum_{t < k} conv[t] for k integer. For strict inequality d3 > d1+d2, i.e., S < d3. So we need for each d3, P(S < d3) = sum_{t=2}^{d3-1} conv[t] (since min sum is 2). Actually min D_i is 1, so min sum = 2. So conv[2] is start.
Alternatively, we can compute prefix sums of conv.
-
Then compute total probability:
P = 0.0
for d3 from 1 to Dmax:
if p[d3] > 0:
P += p[d3] * (cumulative sum of conv for indices < d3)
Note: For d3=1, S >=2 always, so P(S < 1) = 0, so that term zero.
-
Then answer = P (since we computed probability of event D3 > D1+D2). But is that exactly the desired conditional probability? We derived conditional probability = P(D3 > D1 + D2). Since denominator=1/8, numerator = (1/8) * P(D3 > D1+D2)? Wait, careful: We defined Num = probability of joint event (wins condition and sum condition). Then we expressed the event under the conditioned distribution of unordered pairs. Actually, we conditioned on the wins. The distribution we used (D_i i.i.d. from the difference distribution) is exactly the conditional distribution of the unordered pairs given the win outcomes. And the sum condition event is exactly D3 > D1 + D2. So under the conditional distribution, the probability that sum condition holds is P_cond = P(D3 > D1+D2). And the unconditional denominator = 1/8. But our conditional probability is precisely P_cond. Because we are asked: "What is the probability that the sum of the three balls Jerry drew is strictly higher than the sum of the three balls Andrew drew?" given that Andrew wins rounds 1 and 2 and Jerry wins round 3. That is exactly P_cond, the probability under the condition. So answer = P_cond. No need to multiply by 8. Let's confirm:
We have original probability space: all ordered triple of draws. Conditional probability defined as P(E | C) = P(E & C)/P(C). We argued that P(C)=1/8. Then we transformed to conditional distribution: Under condition C, the distribution of unordered pairs is i.i.d. uniform over C(n,2) with assignment as described. So P(E | C) = probability that sum condition holds under that conditional distribution. That is exactly what we computed via D's. So answer = P(D3 > D1 + D2). Good.
But check with sample: n=2, numbers 1,2. Then unordered pairs: only one unordered pair {1,2}, diff=1. So p[1]=1. Then D1, D2, D3 all always =1. Then D3 > D1+D2? 1 > 1+1=2 false. So probability = 0. That matches sample output 0.0.
Second sample: n=3, numbers 1,2,10. Let's compute: M = 3. Unordered pairs: {1,2}: diff=1; {1,10}: diff=9; {2,10}: diff=8. So p[1]=1/3, p[8]=1/3, p[9]=1/3. D1, D2, D3 i.i.d. with these probabilities. Need P(D3 > D1 + D2). Let's enumerate possible (D1,D2) sums:
- (1,1): sum=2, probability (1/3)^2 = 1/9. For D3 must be >2: possibilities D3=8 or 9 -> probability 2/3. So contribution: (1/9)*(2/3)=2/27.
- (1,8): sum=9, prob 2*(1/3)(1/3)? Wait, ordered pairs: probability of D1=1, D2=8 is (1/3)(1/3) = 1/9. Similarly D1=8, D2=1 is 1/9, total for sum=9 is 2/9. For D3 >9, only D3= ? 9 is not >9, need >9. The max diff is 9, so only D3=10? Not existing. So no D3 >9. So contributions 0.
- (1,9): sum=10, prob 2/9? Actually (1,9) and (9,1): each 1/9, total 2/9. D3 >10 impossible -> 0.
- (8,8): sum=16, prob 1/9. D3 >16 impossible.
- (8,9): sum=17, prob 2/9. 0.
- (9,9): sum=18, prob 1/9. 0.
Thus total probability = 2/27 ≈ 0.0740740741, which matches sample output. So our formula works.
Thus answer = P(D3 > D1 + D2). Good.
Now we must compute this efficiently.
Given n up to 2000, maxD up to 5000, O(maxD^2) is okay. But we must be careful: n=2000, maxD could be as high as 5000, 25 million double loop in Python may be borderline but probably fine with PyPy given 1 sec? Typically 25 million simple operations in Python is okay if optimized (maybe around 0.3-0.5 seconds in PyPy? Actually 25 million iterations with two nested loops doing float multiplication and addition might be heavier. But we can improve.
We can compute the convolution more efficiently using FFT? But maxD 5000 is small, we can manage with loops. However, we might also note that many p[d] are zero; we could compress to only non-zero d values. Let K = number of distinct differences. In worst case, if numbers are dense (e.g., all integers 1..2000), differences range from 1 to 1999, and there are about 1999 distinct differences, maybe less. Actually with consecutive numbers, all differences from 1 to 1999 appear. So K ~ 2000. Then double loop over K ~ 2000 gives 4 million, which is fine. But if numbers are spread out (like 1 and 5000, etc.), differences could be many, but still bounded by maxD ~5000. So K <= 5000. But if numbers are sparse with large gaps, number of distinct differences could be less than 5000 because many differences may not appear; but still K could be up to around n^2 if the numbers are such that differences are all distinct? But differences are bounded by 5000, so maximum distinct differences is at most 5000 (since difference is integer between 1 and 5000). Actually difference can be up to 5000, so there are at most 5000 possible distinct values. So K ≤ 5000. So we can compress by creating list of (d, p) for d where p>0. Then double loop over that list: for d1,p1 in list, for d2,p2 in list: conv[d1+d2] += p1*p2. That loops over K^2, which up to 25 million if K=5000. But we can reduce by using that conv is symmetric and we can compute half? Not much.
But maybe we can compute cumulative sums more directly without full convolution: For each d3, we need P(D1