Benny and the Universe
Practice
4 (33 votes)
Breadth first search
Graphs
Medium
Problem
79% Success 3772 Attempts 50 Points 5s Time Limit 256MB Memory 1024 KB Max Code

Hope you still have not forgotten about pig Benny.

She is asking for help again.

This time you have to answer some queries that Benny will give you.

All actions happen in the Universe where each planet has its own id starting from 0. There are infinite amount of planets.

To travel between different planets there are some special spacecrafts. Each spacecraft is characterized by the number di.

If the id of the planet you are currently staying on is idj and you decided you use spacecraft with di you will go to planet with id equal to idj + di.

Benny's parents live nearby the planet with id equal to 0, so you may assume that there exists some spacecraft for which di is not more than 104.

You have to answer Q queries. Each query consists of one single integer x. You have to answer whether it's possible to go from planet with id equal to 0 to planet with id equal to x.

Constraints

1 <= N <= 10^3

1 <= Q <= 10^5

1 <= Di <= 10^9

1 <= X <= 10^9

Input

The first line contains two integers denoting N and Q.

The second line contains N integers denoting di for spacecrafts. Numbers might be separated with more than one space and not more than 10.

The following Q lines contain single integer denoting xi.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
11 votes
Tags:
GraphsAlgorithmsBreadth-first searchTree
Points:50
8 votes
Tags:
GraphsAlgorithmsBreadth-first searchC++
Points:50
35 votes
Tags:
MediumShortest Path Algorithm