#P9379. [THUPC 2023 决赛] 老虎机
[THUPC 2023 决赛] 老虎机
Problem Description
Xiao I runs an arcade, and a new type of slot machine has recently become very popular.
As the operator, Xiao I first needs to set the state of the slot machine. The state is a triple , where:
- is a positive integer.
- is a non-empty set of strings, where every string is a length- binary string.
- is a length- real sequence , where for any , .
After setting the state, the game can start. Each round of the game works as follows:
- The player first obtains the slot machine state .
- Internally, the slot machine chooses a string as the answer string. The player needs to obtain the answer string through several interactions with the machine.
- In each interaction, the player inserts one coin and pulls the lever. Then the machine displays an information string of length . For , equals with probability , and equals
?with probability . - All random processes used to generate information strings during interactions are pairwise independent.
- In each interaction, the player inserts one coin and pulls the lever. Then the machine displays an information string of length . For , equals with probability , and equals
- Once the player can uniquely determine the answer string based on the slot machine state and the information strings obtained from interactions, they may input the answer string into the machine to end the game and receive the reward.
Xiao I has set a state, but has not decided how large the reward should be. To match the reward with the difficulty, Xiao I wants to know: for each string in , if the player plays with an optimal strategy (that is, they end the game as soon as the answer string can be uniquely determined), and the answer string is , what is the expected number of coins the player needs to insert.
Since Xiao I does not like real numbers, you need to output the answer modulo .
Input Format
This problem has multiple test cases. The first line contains an integer indicating the number of test cases, followed by the input for each test case.
For each test case:
- The first line contains two integers , denoting the string length and the size of .
- The second line contains integers , where .
- The next lines each contain a length- binary string , describing a string in .
Output Format
For each test case, output lines. The -th line should be the expected number of coins the player needs to insert when the answer string is under the optimal strategy, modulo . The problem guarantees that this value always exists in the modular sense.
4
1 2
5000
0
1
2 3
1 10000
00
01
10
1 1
1
1
3 4
8888 7777 5555
000
010
101
110
2
2
10000
1
10000
0
209031157
194428714
835313860
674719626
Hint
[Sample Explanation #1]
- For the first test case, in each interaction there is a probability of to know whether the answer string is or , and a probability of to obtain no information. Therefore the expected number of coins is .
- For the second test case, in each interaction you can always obtain the second bit of the string, and you obtain the first bit with probability . When the second string is the answer string, it can be uniquely determined by the second bit, while for the other two strings, you must obtain the first bit.
- For the third test case, since , no interaction is needed to determine the answer string.
- For the fourth test case, I have a brilliant explanation, but this space is too small to write it down.
[Constraints]
For all testdata, , , , , and are pairwise distinct length- binary strings.
[Afterword]
“Hey, hey, hey, minors are not allowed to enter the arcade! What? You say you want to go in to learn competitive programming algorithms? Who would believe that nonsense!”
[Source]
From the finals of the 2023 Tsinghua University Student Programming Contest and Collegiate Invitational (THUPC2023).
Solutions and other resources can be found at https://github.com/THUSAAC/THUPC2023.
Translated by ChatGPT 5