#P9379. [THUPC 2023 决赛] 老虎机

    ID: 10542 远端评测题 2500ms 1024MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2023O2优化条件概率期望THUPC状压 DP

[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 (l,S,p)(l,S,\mathbf{p}), where:

  • ll is a positive integer.
  • SS is a non-empty set of strings, where every string is a length-ll binary string.
  • p\mathbf{p} is a length-ll real sequence p0,p1,,pl1p_0,p_1,\dots,p_{l-1}, where for any 0il10 \le i \le l - 1, 0<pi10 < p_i \le 1.

After setting the state, the game can start. Each round of the game works as follows:

  • The player first obtains the slot machine state (l,S,p)(l,S,\mathbf{p}).
  • Internally, the slot machine chooses a string sSs \in S 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 tt of length ll. For 0il10 \le i \le l - 1, tit_i equals sis_i with probability pip_i, and equals ? with probability (1pi)(1-p_i).
    • All random processes used to generate information strings during interactions are pairwise independent.
  • 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 tt in SS, 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 tt, 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 998244353998244353.

Input Format

This problem has multiple test cases. The first line contains an integer TT indicating the number of test cases, followed by the input for each test case.

For each test case:

  • The first line contains two integers l,nl,n, denoting the string length and the size of SS.
  • The second line contains ll integers c0,c1,,cl1c_0,c_1,\dots,c_{l-1}, where pi=ci104p_i = \frac{c_i}{10^4}.
  • The next nn lines each contain a length-ll binary string sis_i, describing a string in SS.

Output Format

For each test case, output nn lines. The ii-th line should be the expected number of coins the player needs to insert when the answer string is sis_i under the optimal strategy, modulo 998244353998244353. 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 500010000=12\frac{5000}{10000} = \frac{1}{2} to know whether the answer string is 00 or 11, and a probability of 12\frac{1}{2} to obtain no information. Therefore the expected number of coins is i=1+i2i=2\sum_{i=1}^{+\infty} \frac{i}{2^i} = 2.
  • 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 110000\frac{1}{10000}. 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 S=1|S| = 1, 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, 1T101 \le T \le 10, 1l151 \le l \le 15, 1n2l1 \le n \le 2^l, 1ci1041 \le c_i \le 10^4, and s1,,sns_1,\dots,s_n are pairwise distinct length-ll 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