Given four of the six group matches, compute the probability the starred team finishes in the top two, including tie-breaking and random lots.
Medium7ProbabilityCombinatoricsImplementationMathNo attempts yetTime limit2sMemory limit512 MBAfter the FIFA World Cup, a larger competition called GIGA Universe Cup takes place somewhere in our universe. Both competitions have two rounds: the first round, called the group league, and the second round, called the final tournament. In the first round the participating teams are split into groups of four teams. Every team in a group plays one match against each of the other three teams of the same group. If a group holds Engband, Swedon, Argontina and Nigerua, the group plays these six matches: Engband vs Swedon, Engband vs Argontina, Engband vs Nigerua, Swedon vs Argontina, Swedon vs Nigerua, and Argontina vs Nigerua.
The result of one match is the number of goals each team scored. Engband 1 - 0 Argontina says that Engband scored one goal and Argontina scored none. The result gives points to the two teams, and the points rank the teams. A team that scores more goals than the other wins the match and gets three points, and the other team gets zero points. If both teams score the same number of goals the match is a draw and each team gets one point.
The goal difference of a team over a set of matches is the total number of goals the team scored in those matches minus the total number of goals its opponents scored in the same matches. Over the three matches Swedon 1 - 2 Engband, Swedon 3 - 4 Nigerua and Swedon 5 - 6 Argontina, the goal difference of Swedon is (1+3+5)−(2+4+6)=−3.
Once all six matches of a group are played, the teams are ranked by the criteria below, listed in the order of priority. Criterion (a) decides the ranking first, ties are broken by (b), remaining ties by (c).
(a) greater number of points in all the group matches
(b) greater goal difference in all the group matches
(c) greater number of goals scored in all the group matches
If two or more teams are still equal after those three criteria, their places are decided by the following criteria, applied in this order:
(d) greater number of points obtained in the group matches between the teams concerned
(e) greater goal difference resulting from the group matches between the teams concerned
(f) greater number of goals scored in the group matches between the teams concerned
If two or more teams are still equal, apply (d), (e) and (f) again to each set of teams that remain equal, counting only the matches between the teams of that set. Repeat this until the three criteria separate no one further. Teams that are still equal are ordered by:
(g) drawing lots by the organizing committee of the GIGA Universe Cup.
The two teams coming first and second in each group qualify for the second round.
Write a program that reads the results of the matches played so far in one group together with one designated team of that group, and computes the probability that the designated team qualifies for the second round. Every team has played exactly two matches and has one match left to play, so four matches of the group are played and two matches are still to come.
The probability that a team scores exactly p goals in a match is
p!(8−p)!8!(41)p(43)8−p
for p≤8, and zero for p>8. The four goal counts of the two remaining matches are independent. The lot in step (g) is fair, so every order of the teams that remain equal has the same probability.
The first line holds an integer less than 1000, the number of records that follow.
The rest of the input is that number of records. One record has five lines with this layout:
<empty> <_> <team1> <_> <team2> <_> <team3> <_> <team4>
<team1> <_> <empty> <_> <m12> <_> <m13> <_> <m14>
<team2> <_> <empty> <_> <empty> <_> <m23> <_> <m24>
<team3> <_> <empty> <_> <empty> <_> <empty> <_> <m34>
<team4> <_> <empty> <_> <empty> <_> <empty> <_> <empty>
The layout above carries spaces only for readability. In the input each line is the five fields joined without any space, so each line is exactly 24 characters long. Here <_> is a single underscore and <empty> is a sequence of exactly four underscores.
Each of <team1> to <team4> is an asterisk followed by exactly three uppercase letters, such as *ENG, or an underscore followed by exactly three uppercase letters, such as _SWE. The asterisk marks the team whose probability of qualification you have to compute. Exactly one of the four teams carries the asterisk, and the four team codes are distinct.
Each <mij> with 1≤i<j≤4 is the result of the match between <teami> and <teamj>. A match result is either __-_, that is two underscores, a hyphen and one more underscore, or _x-y where x and y are single digits not greater than 8. The first form means the match has not been played yet. The second form means the match ended with x goals by <teami> and y goals by <teamj>. Every team has played exactly two matches, so exactly two match results are in the first form.
Print n lines where n is the number of records in the input. Line i holds the probability that the team marked with an asterisk in record i qualifies for the second round.
The exact probability is a rational number. Round that exact value to seven decimal places, rounding a value that sits exactly halfway up, and print it with exactly seven digits after the decimal point.