problems / 245

Divisible by At Least One

medium inclusion-exclusion number-theory gcd bitmask math 2000 ms · 256 MB

Divisible by At Least One

You are given k positive integers m_1, m_2, …, m_k and q queries. Each query is a positive integer N: count how many integers x with 1 ≤ x ≤ N are divisible by at least one of the m_i.

The m_i need not be distinct, and one may divide another.

Input

The first line contains two integers k and q (1 ≤ k ≤ 15, 1 ≤ q ≤ 2000).

The second line contains k integers m_1, …, m_k (1 ≤ m_i ≤ 10^9).

Each of the next q lines contains one integer N (1 ≤ N ≤ 10^18).

Output

For each query, print on its own line the number of integers in [1, N] divisible by at least one m_i. The answer never exceeds 10^18, so it fits in a signed 64-bit integer.

Notes

In the first example, the numbers up to 12 divisible by 4 or 6 are 4, 6, 8 and 12. Note that 12 is divisible by both but is counted once. No number in [1, 1] qualifies. Up to 24 there are 6 multiples of 4 and 4 multiples of 6, and 12 and 24 are in both lists, so the answer is 6 + 4 − 2 = 8.

In the second example, every multiple of 4 is already a multiple of 2, and the repeated 2 changes nothing, so the answer is the number of even integers up to N.

Example 1

Input

2 3
4 6
12
1
24

Output

4
0
8

Example 2

Input

3 2
2 4 2
10
1000000000000000000

Output

5
500000000000000000

Hints · reveal one at a time if you're stuck

Hint 1

Counting multiples of a single number up to N is one division. If you add those counts for every m_i, which numbers get counted too many times?

Hint 2

Use inclusion–exclusion over subsets of the moduli. The numbers divisible by every modulus in a subset are exactly the multiples of one number: which one? (It isn't always the product.)

Hint 3

The subset values don't depend on N, so compute them once, before the queries. Any value above 10^18 contributes nothing; detect that before the 64-bit multiplication overflows.

report an issue Prefer your own editor? sooks prep 245 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.