#K81242. Find the Smallest Prime Greater Than or Equal to Query

    ID: 35710 Type: Default 1000ms 256MiB

Find the Smallest Prime Greater Than or Equal to Query

Find the Smallest Prime Greater Than or Equal to Query

You are given an array of integers and a list of queries. For each query, you need to determine the smallest prime number in the array that is greater than or equal to the given query value. If no such prime exists in the array, output -1.

The problem involves two main steps:

  1. Filter the array to retain only the prime numbers. To verify the primality, you may use a sieve method up to \(10^6\).
  2. Search within the filtered array to find, for each query \(q\), the smallest prime \(p\) such that \(p \ge q\). Binary search is recommended if the filtered array is sorted.

If the array does not contain any prime number satisfying the condition for a query, print \(-1\).

Note: All formulas are expressed in \( \LaTeX \) format. For example, the condition is \( p \ge q \).

inputFormat

The first line of input contains two space-separated integers \(N\) and \(Q\), where \(N\) is the number of elements in the array and \(Q\) is the number of queries.

The second line contains \(N\) space-separated integers representing the array.

The third line contains \(Q\) space-separated integers representing the queries.

outputFormat

For each query, output a single line with the smallest prime number from the array that is greater than or equal to the query value. If there is no such prime, output \(-1\).

## sample
6 3
2 3 5 7 11 13
4 6 12
5

7 13

</p>