#P13072. [GCJ 2020 Finals] Hexacoin Jam
[GCJ 2020 Finals] Hexacoin Jam
题目描述
The Code Jam team's first cryptocurrency, jamcoins, never caught on. This year, we are trying again with , which are named for their use of base 16. To "mine" a -digit hexacoin, one has to work with integers using exactly base 16 digits, including leading zeroes if needed. Each value represents an integer between 0 and , 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 , corresponding to the base 10 values 3883, 200 and 0. On the other hand, 1234, DF, C0DE and JAM are not valid values when .
When performing addition of -digit base 16 values, any overflow digits are dropped. That is, the addition is performed modulo . 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 yields 3670).
To "mine" a -digit hexacoin, a computer must perform the following steps:
- Choose a list of -digit base 16 values $\mathbf{L}_1, \mathbf{L}_2, ..., \mathbf{L}_\mathbf{N}$.
- Choose a target range of -digit base 16 values: the numbers from to , inclusive.
- Choose a permutation of the base 16 digits 0 through F, uniformly at random from among all 16! such permutations.
- Apply to all digits of all numbers in the list, creating a new list consisting of -digit base 16 values. Formally, the -th digit of the -th element of is the result of applying to the -th digit of the -th element of .
- Choose a pair of elements from without replacement, uniformly at random from among all such possible choices, and independently of the choice of permutation.
- Calculate the sum (dropping overflow digits) of the two chosen elements.
If the sum calculated in the last step is between and , inclusive, then a hexacoin has been found! For example, suppose that:
- .
- and .
- 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 is applied to , the resulting is [A89, 444, DD3, 444, 001]. Notice that is not applied to and .
There are pairs of values to choose, and each pair has a probability of 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 and the range 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, . test cases follow. Each test case consists of three lines. The first line contains two integers and : the size of the given list and the number of digits to work with, respectively. The second line contains two -digit base 16 numbers and : the inclusive lower and upper bounds of the target range, respectively. Then there is one more line containing -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 is the test case number (starting from 1) and and are non-negative integers, such that the fraction represents the probability of finding a hexacoin, under the conditions described above. All of , , and must be in base 10. If there are multiple acceptable values for and , choose the ones such that 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 . Since the result ends with , the sum of the values assigned to both last digits and must end in as well. Since and are different values, their sum cannot be exactly . Therefore, must be (in base 16). There are pairs of different digits that accomplish that. and cannot both be . All pairs lead to an overall sum of (after dropping an overflow ). Therefore, there are assignments of different digits to and that lead to a hexacoin. There are possible assignments to those digits, so the result is .
In Sample Case #2, we need to add the probability of the result being exactly to the result of Sample Case #1. The only way that happens is if and are assigned to and , in either order. That has a probability of , leading to a total of .
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 . Since the only value in range is odd, we have no hope of mining a hexacoin in this case. Notice that is an invalid representation of the answer because would not be minimum.
Limits
- .
- contains exactly characters.
- Each character of is a base 16 digit.
- contains exactly characters.
- Each character of is a base 16 digit.
- .
- contains exactly characters, for all .
- Each character of is a base 16 digit, for all .
Test Set 1 (10 Pts, Visible Verdict)
- .
- .
Test Set 2 (10 Pts, Hidden Verdict)
- .
- .
Test Set 3 (22 Pts, Hidden Verdict)
- .
- .