#P13072. [GCJ 2020 Finals] Hexacoin Jam

    ID: 12882 Type: RemoteJudge 90000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 7 Uploaded By: Tags>数学2020组合数学Google Code Jam

[GCJ 2020 Finals] Hexacoin Jam

题目描述

The Code Jam team's first cryptocurrency, jamcoins, never caught on. This year, we are trying again with hexacoinshexacoins, which are named for their use of base 16. To "mine" a D\mathbf{D}-digit hexacoin, one has to work with integers using exactly D\mathbf{D} base 16 digits, including leading zeroes if needed. Each value represents an integer between 0 and 16D116^{\mathbf{D}} - 1, inclusive. Base 16 digits are represented by the numbers 0 through 9 and the uppercase letters A through F, as usual. For example, F2B, 0C8 and 000 are valid values when D=3\mathbf{D}=3, corresponding to the base 10 values 3883, 200 and 0. On the other hand, 1234, DF, C0DE and JAM are not valid values when D=3\mathbf{D}=3.

When performing addition of D\mathbf{D}-digit base 16 values, any overflow digits are dropped. That is, the addition is performed modulo 16D16^{\mathbf{D}}. For example, F2B + 0C8 = FF3 (4083 in base 10) and F2B + F2B = E56 (3670 in base 10, because the sum's result is 7766, and taking modulo 16316^3 yields 3670).

To "mine" a D\mathbf{D}-digit hexacoin, a computer must perform the following steps:

  1. Choose a list L\mathbf{L} of N\mathbf{N} D\mathbf{D}-digit base 16 values $\mathbf{L}_1, \mathbf{L}_2, ..., \mathbf{L}_\mathbf{N}$.
  2. Choose a target range of D\mathbf{D}-digit base 16 values: the numbers from S\mathbf{S} to E\mathbf{E}, inclusive.
  3. Choose a permutation P\mathbf{P} of the base 16 digits 0 through F, uniformly at random from among all 16! such permutations.
  4. Apply P\mathbf{P} to all digits of all numbers in the list, creating a new list L\mathbf{L}' consisting of N\mathbf{N} D\mathbf{D}-digit base 16 values. Formally, the jj-th digit of the ii-th element of L\mathbf{L}' is the result of applying P\mathbf{P} to the jj-th digit of the ii-th element of L\mathbf{L}.
  5. Choose a pair of elements from L\mathbf{L}' without replacement, uniformly at random from among all such possible choices, and independently of the choice of permutation.
  6. Calculate the sum (dropping overflow digits) of the two chosen elements.

If the sum calculated in the last step is between S\mathbf{S} and E\mathbf{E}, inclusive, then a hexacoin has been found! For example, suppose that:

  • L=[134,000,FFB,000,AA9]\mathbf{L} = [134, 000, FFB, 000, AA9].
  • S=85C\mathbf{S} = 85C and E=EDF\mathbf{E} = EDF.
  • The computer happens to choose $\mathbf{P} = (0 \rightarrow 4, 1 \rightarrow A, 2 \rightarrow 2, 3 \rightarrow 8, 4 \rightarrow 9, 5 \rightarrow B, 6 \rightarrow C, 7 \rightarrow 7, 8 \rightarrow F, 9 \rightarrow 1, A \rightarrow 0, B \rightarrow 3, C \rightarrow 5, D \rightarrow 6, E \rightarrow E, F \rightarrow D)$.

Then, when P\mathbf{P} is applied to L\mathbf{L}, the resulting L\mathbf{L}' is [A89, 444, DD3, 444, 001]. Notice that P\mathbf{P} is not applied to S\mathbf{S} and E\mathbf{E}.

There are (5×4)/2=10(5 \times 4) / 2 = 10 pairs of values to choose, and each pair has a probability of 1/101/10 of being chosen. The only sums that fall within the range are A89 + DD3 = 85C, 444 + 444 = 888, A89 + 001 = A8A, DD3 + 001 = DD4, and A89 + 444 = ECD (twice).

The first two steps are already computed and you know the list L\mathbf{L} and the range [S,E][\mathbf{S}, \mathbf{E}] that were chosen. What is the probability that a hexacoin is found after the rest of the process is performed?

输入格式

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each test case consists of three lines. The first line contains two integers N\mathbf{N} and D\mathbf{D}: the size of the given list and the number of digits to work with, respectively. The second line contains two D\mathbf{D}-digit base 16 numbers S\mathbf{S} and E\mathbf{E}: the inclusive lower and upper bounds of the target range, respectively. Then there is one more line containing N\mathbf{N} D\mathbf{D}-digit base 16 numbers $\mathbf{L}_1, \mathbf{L}_2, \dots, \mathbf{L}_\mathbf{N}$, representing the values in the list.

输出格式

For each test case, output one line containing Case #x: y z, where xx is the test case number (starting from 1) and yy and zz are non-negative integers, such that the fraction y/zy/z represents the probability of finding a hexacoin, under the conditions described above. All of xx, yy, and zz must be in base 10. If there are multiple acceptable values for yy and zz, choose the ones such that zz is minimized.

4
2 2
10 10
00 FF
2 2
10 11
00 FF
4 3
FFF FFF
230 A10 010 F70
4 3
AFF FFF
230 A10 010 F70
Case #1: 7 120
Case #2: 1 15
Case #3: 0 1
Case #4: 2731 8736

提示

Sample Explanation

In Sample Case #1, the target range is just a single value 1010. Since the result ends with 00, the sum of the values assigned to both last digits 00 and FF must end in 00 as well. Since P[0]\mathbf{P}[0] and P[F]\mathbf{P}[F] are different values, their sum cannot be exactly 00. Therefore, P[0]+P[F]\mathbf{P}[0] + \mathbf{P}[F] must be 1010 (in base 16). There are 77 pairs of different digits that accomplish that. P[0]\mathbf{P}[0] and P[F]\mathbf{P}[F] cannot both be 88. All 77 pairs lead to an overall sum of 1010 (after dropping an overflow 11). Therefore, there are 1414 assignments of different digits to 00 and FF that lead to a hexacoin. There are 16×1516 \times 15 possible assignments to those digits, so the result is 14/240=7/12014/240 = 7/120.

In Sample Case #2, we need to add the probability of the result being exactly 1111 to the result of Sample Case #1. The only way that happens is if 00 and FF are assigned to 00 and 11, in either order. That has a probability of 2/240=1/1202/240 = 1/120, leading to a total of 7/120+1/120=8/120=1/157/120 + 1/120 = 8/120 = 1/15.

In Sample Case #3, notice that regardless of which permutation and pair of numbers the computer chooses from the list, we will add two numbers that end in the same digit. That produces an even result, even after taking it modulo 16316^3. Since the only value in range is odd, we have no hope of mining a hexacoin in this case. Notice that 0/20/2 is an invalid representation of the answer because zz would not be minimum.

Limits

  • 2N4502 \leq \mathbf{N} \leq 450.
  • S\mathbf{S} contains exactly D\mathbf{D} characters.
  • Each character of S\mathbf{S} is a base 16 digit.
  • E\mathbf{E} contains exactly D\mathbf{D} characters.
  • Each character of E\mathbf{E} is a base 16 digit.
  • SE\mathbf{S} \leq \mathbf{E}.
  • Li\mathbf{L}_i contains exactly D\mathbf{D} characters, for all ii.
  • Each character of Li\mathbf{L}_i is a base 16 digit, for all ii.

Test Set 1 (10 Pts, Visible Verdict)

  • 1T1001 \leq \mathbf{T} \leq 100.
  • 2D32 \leq \mathbf{D} \leq 3.

Test Set 2 (10 Pts, Hidden Verdict)

  • 1T1001 \leq \mathbf{T} \leq 100.
  • 2D42 \leq \mathbf{D} \leq 4.

Test Set 3 (22 Pts, Hidden Verdict)

  • 1T101 \leq \mathbf{T} \leq 10.
  • 2D52 \leq \mathbf{D} \leq 5.