#P7670. [JOI 2018 Final] 毒蛇越狱 / Snake Escaping

[JOI 2018 Final] 毒蛇越狱 / Snake Escaping

Description

There are 2L2^L poisonous snakes in the JOI laboratory. The snakes are numbered 0,1,,2L10,1,\cdots,2^L-1. Each snake is divided into LL segments from head to tail. Each segment is either blue or red. For snake ii, let i=k=1Lck2Lki = \sum_{k=1}^{L} c_k2^{L-k} (0ck10 \leq c_k \leq 1) be the binary representation of ii. Then:

  • If ck=0c_k=0, the color of the kk-th segment from the head of snake ii is blue.
  • If ck=1c_k=1, the color of the kk-th segment from the head of snake ii is red.

Each poisonous snake has an integer toxicity between 00 and 99, inclusive. You are given a string SS of length 2L2^L consisting of 0,1,2,3,4,5,6,7,8,9\texttt{0,1,2,3,4,5,6,7,8,9}. The ii-th character (1i2L1 \leq i \leq 2^L) is the toxicity of snake i1i-1.

Because the snakes move quickly, they often escape from the JOI laboratory. People living near the laboratory complain to the JOI laboratory that they saw snakes escaping. You will receive a list of complaints for QQ days. The complaint on day dd (1dQ1 \leq d \leq Q) is a string TdT_d of length LL consisting of 0,1,?\texttt{0,1,?}.

  • If the jj-th character (1jL1 \leq j \leq L) of TdT_d is 0\texttt{0}, it means that for every snake that escaped from the laboratory on day dd, its jj-th segment is blue.
  • If the jj-th character (1jL1 \leq j \leq L) of TdT_d is 1\texttt{1}, it means that for every snake that escaped from the laboratory on day dd, its jj-th segment is red.
  • If the jj-th character (1jL1 \leq j \leq L) of TdT_d is ?\texttt{?}, it means that people did not provide information about the jj-th segment of the snakes that escaped on day dd.

All complaints are accurate. All snakes that escape from the laboratory are taken in by the laboratory staff on the same day. It is possible that the same snake escapes on different days.

To estimate the risk of snakes escaping, Director Professor K wants to know the total toxicity of the snakes that could have escaped from the laboratory. Your task is to write a program that, based on the list of complaints for QQ days, computes for each day the total toxicity of snakes that could have escaped from the laboratory.

Given the string SS describing the snakes’ toxicities and the list of complaints for QQ days, write a program to compute, for each day, the total toxicity of the snakes that could have escaped.

Input Format

The first line contains two space-separated integers LL and QQ, representing the number of segments of each snake and the number of complaint days. The second line contains a string SS of length 2L2^L, describing the toxicities of the snakes. The next QQ lines each contain a string TdT_d of length LL, which is the complaint for day dd.

Output Format

Output QQ lines. The dd-th line should contain one integer: the total toxicity of the snakes that could have escaped from the laboratory on day dd.

3 5
12345678
000
0??
1?0
?11
???
1
10
12
12
36
4 8
3141592653589793
0101
?01?
??1?
?0??
1?00
01?1
??10
????
9
18
38
30
14
15
20
80

Hint

Constraints

For 100%100\% of the testdata, 1L201 \leq L \leq 20, 1Q1061 \leq Q \leq 10^6, SS is a string of length 2L2^L consisting of 0,1,2,3,4,5,6,7,8,9\texttt{0,1,2,3,4,5,6,7,8,9}, and each TdT_d is a string of length LL (1dQ1 \leq d \leq Q) consisting of 0,1,?\texttt{0,1,?}.

  • Subtask 11 (55 points): L10L \leq 10, Q1000Q \leq 1000.
  • Subtask 22 (77 points): L10L \leq 10.
  • Subtask 33 (1010 points): L13L \leq 13.
  • Subtask 44 (5353 points): Q5000Q \leq 5000.
  • Subtask 55 (2525 points): no additional constraints.

Sample Explanation

For Sample 11: L=3L=3, so there are 23=82^3=8 poisonous snakes, and each is divided into 33 segments. There are 55 days of complaints.

  • On the first day, the only snake that could have escaped is snake 00. The total toxicity is 11.
  • On the second day, the snakes that could have escaped are snakes 0,1,2,30,1,2,3. The total toxicity is 1010.
  • On the third day, the snakes that could have escaped are snakes 4,64,6. The total toxicity is 1212.
  • On the fourth day, the snakes that could have escaped are snakes 3,73,7. The total toxicity is 1212.
  • On the fifth day, the snakes that could have escaped are snakes 0,1,2,3,4,5,6,70,1,2,3,4,5,6,7. The total toxicity is 3636.

Problem Notes

This problem is from T5: Snake Escaping in the Final Round of The 17th Japanese Olympiad in Informatics (JOI 2017/2018).
Translated and organized by @求学的企鹅.

Translated by ChatGPT 5