Monk and Divisor Conundrum

4.3

20 votes
Mathematics, Factorization
Problem

Here is another task for you, prepared by Monk himself. So, this is how it goes :

Given an integer array A of size N, Monk needs you to answer T queries for him. In each query, he gives you 2 integers P and Q. In response to each of these queries, you need to tell Monk the count of numbers in array A. that are either divisible by P, Q, or both.

Can you cope with this ?

Video approach to solve this question: here

Input Format :

The first line contains a single integer N denoting the size of array A. The next line contains N space separated integers, where the ith integer denotes A[i].

The next line contains a single integer T denoting the number of queries Monk poses to you. Each of the next T lines contains 2 space separated integers P and Q.

Output Format :

For each query, print the answer on a new line.

Constraints :

1N2×105

1A[i]2×105

1T2×105

1P,Q2×105

Sample Input
6
2 3 5 7 4 9
2
4 5
3 7
Sample Output
2
3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the 1st query in the 1st sample, the numbers that form a part of the count are 4 and 5, present at indices 5 and 3 respectively.

Contributers:
Editor Image

?