Snake Escaping
Time limit2sMemory limit64 MB
Given a toxicity value for each of 2^L bitmasks, answer Q queries: each query fixes some bits and leaves others free, and asks the sum of values over all matching masks.
- Level
Medium7 of 10
- Topics
- Bit manipulation, Prefix sum, Dynamic programming, Math
- Solved
- No attempts yet
Problem
JOI Laboratory keeps poisonous snakes, numbered . Each snake is divided into parts from the head to the tail, and each part is either blue or red. Write the number of snake in binary as (). Then
- if , the -th part of snake from the head is blue, and
- if , the -th part of snake from the head is red.
Each snake has an integer between and , inclusive, called its toxicity. You are given a string of length consisting of the digits to . The -th character of () is the toxicity of snake .
Snakes are quick, so they often escape from JOI Laboratory. People living near the laboratory file complaints when they see snakes escaping.
You are given the complaints for days. The complaint for the -th day () is a string of length consisting of the characters , , and .
- If the -th character of () is , the -th part of every snake that escaped on the -th day is blue.
- If the -th character of is , the -th part of every snake that escaped on the -th day is red.
- If the -th character of is , nobody reported anything about the -th part of the snakes that escaped on the -th day.
Every complaint is accurate. All snakes that escaped were caught by the laboratory staff on the same day, so the same snake may escape again on a different day.
To estimate the risk of escaping snakes, Professor K, the executive director of JOI Laboratory, wants to know, for each day, the sum of the toxicities of the snakes that might have escaped. That is, for the -th day you must add up the toxicities of all snakes that do not contradict .
Given the string describing the toxicities and the complaints for days, write a program that computes, for each day, the sum of the toxicities of the snakes that might have escaped from the laboratory.
Note that the memory limit for this task is small.
Input
Read the following data from standard input.
- The first line contains two integers and separated by a space: the number of parts of each snake and the number of days with complaints.
- The second line contains a string of length describing the toxicities of the snakes.
- The -th of the following lines () contains a string of length , the complaint for the -th day.
Output
Write lines to standard output. The -th line must contain one integer: the sum of the toxicities of the snakes that might have escaped on the -th day.
Constraints
- is a string of length .
- consists only of the characters .
- is a string of length ().
- consists only of the characters ().