#P7670. [JOI 2018 Final] 毒蛇越狱 / Snake Escaping
[JOI 2018 Final] 毒蛇越狱 / Snake Escaping
Description
There are poisonous snakes in the JOI laboratory. The snakes are numbered . Each snake is divided into segments from head to tail. Each segment is either blue or red. For snake , let () be the binary representation of . Then:
- If , the color of the -th segment from the head of snake is blue.
- If , the color of the -th segment from the head of snake is red.
Each poisonous snake has an integer toxicity between and , inclusive. You are given a string of length consisting of . The -th character () is the toxicity of snake .
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 days. The complaint on day () is a string of length consisting of .
- If the -th character () of is , it means that for every snake that escaped from the laboratory on day , its -th segment is blue.
- If the -th character () of is , it means that for every snake that escaped from the laboratory on day , its -th segment is red.
- If the -th character () of is , it means that people did not provide information about the -th segment of the snakes that escaped on day .
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 days, computes for each day the total toxicity of snakes that could have escaped from the laboratory.
Given the string describing the snakes’ toxicities and the list of complaints for 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 and , representing the number of segments of each snake and the number of complaint days. The second line contains a string of length , describing the toxicities of the snakes. The next lines each contain a string of length , which is the complaint for day .
Output Format
Output lines. The -th line should contain one integer: the total toxicity of the snakes that could have escaped from the laboratory on day .
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 of the testdata, , , is a string of length consisting of , and each is a string of length () consisting of .
- Subtask ( points): , .
- Subtask ( points): .
- Subtask ( points): .
- Subtask ( points): .
- Subtask ( points): no additional constraints.
Sample Explanation
For Sample : , so there are poisonous snakes, and each is divided into segments. There are days of complaints.
- On the first day, the only snake that could have escaped is snake . The total toxicity is .
- On the second day, the snakes that could have escaped are snakes . The total toxicity is .
- On the third day, the snakes that could have escaped are snakes . The total toxicity is .
- On the fourth day, the snakes that could have escaped are snakes . The total toxicity is .
- On the fifth day, the snakes that could have escaped are snakes . The total toxicity is .
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
京公网安备 11011102002149号