Count All Factors

3.7

14 votes
Number Theory, Integer Factorization, Math, Algorithms, Number theory
Problem

You are given an array a of size n having distinct prime integers.

You will be given q queries. In each query, you will be given an integer k and you have to count the sum of Perfect Factors of the factors of this number.

Perfect Factors: Let's suppose an integer k. Suppose z1,z2,...,zt (t = number of factors) are the factors of integer k. Let's consider one of the factor, say zi. We now calculate how many elements in the provided array (a) perfectly divide this number. Suppose ci is the number of elements in the array that are perfectly dividing the factor zi. Therefore, number of perfect factors for factor zi = ci. So sum of perfect factors of all the factors of number k is c1+c2+...+ct.

Input Format:

  • The first line contains one integer n  size of array a.
  • The second line contains n space - seperated integers a[1],a[2],...,a[n]  denoting the elements of a.
  • The third line contains one integer q  number of queries. Then q queries follow.
  • The first and only line of each query contains a single integer k.

Output Format:

For each query, output in a separate line:

The sum of Perfect Factors of the factors of the number.

Constraints:

1n105
2ai9999889
1q106
1k107
It is guaranteed that all elements of a are distinct and are prime numbers.

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

For the sixth query, k= 6. Factors of 6 are: 1,2,3,6. Number of elements of array a that perfectly divides 1 are 0. Similarly for 2,3,6 it is 1,1,2 respectively. Therefore, the answer for this query is 0+1+1+2=4.

Editor Image

?