A pair of positive integers (a, b) is said to be a coprime-twin pair, if for all positive integers x, both a and b and are coprime to x or both and are not coprime to x. Formally, 2 distinct positive integers a and b can form a coprime-twin pair if and only if S(a) = S(b), where S(x) denotes the set of all positive integers that are coprime to x.
For example:2 and 4 are coprime-twin pairs.
1 and 2 are not coprime-twin pairs.
The score of a sequence a1, a2, .. an is the number of indices (i, j) such that i < j and the pair (ai, aj) forms a coprime-twin pair.
You are given an array A of positive integers and Q queries of the form L, R. For each query, determine the score of the subarray [L, R] inclusive.
Note: A subarray is a contiguous non-empty segment of the array.
The first line contains a single integer ‘T’ denoting the number of test cases to be run. Then the test cases follow.
The first line of each test case contains two space-separated integers N and Q which denote the number of elements in the array A and the number of queries you need to answer respectively.
The next line contains N space-separated integers denoting the array a1, a2, … an.
The next Q lines contain two integers each - Li and Ri, the parameters of the ith query.
Output Format:
For each test case, Q lines
For each of the next Q lines, print a single integer denoting the answer for the ith query.
Note:
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N, Q <= 10^4
1 <= Ai <= 10^6
1 <= L <= R <= N
Time Limit: 1 sec.
1
6 4
2 4 6 8 6 4
3 5
2 4
1 4
1 5
1
1
3
4
For the first query, (6,6) is the coprime twin pair.
For the second query, (4, 8) is the twin pair.
For the third query, (2, 4), (2, 8) and (4, 8) are the co primes twins.
For the 4th query, (2, 4), (2, 8),(6, 6) and (4, 8) are the required pairs.
2
1 1
7
1 1
2 1
3 9
1 2
0
1
Think of relating the set of primes of two numbers if they are coprime twins.
Two numbers are called co-prime twins if and only if the set of primes in its prime factorization is equal.
So for any number, we need to reduce any power of a prime to only single if we want to compare the sets.
If x = p1^x1*p2^x2 … pk^xk
we can reduce it to
x = p1 * p2 * p, .. pk
Now, this can be done using pre-computation of primes using a sieve of Eratosthenes.
Now we just need to count for each query the number of equal pairs in that range which can be done using a count array..
Algorithm:
coprimeTwins (Q, A) returns the answer for each of the queries in Q.
precompute(reduced) returns an array where each index stores its reduced form.
O(MAX*log(MAX) + Q*N) where MAX is the maximum value of Ai in the input array and ‘Q’ is the number of queries and N is the size of the array.
We spend MAX * log(MAX) time for a pre-computation part for calculating the prime factors and O(N) time for finding the result of each query.
O(MAX),where MAX is the maximum value of Ai in the input array .
O(MAX), Since we are storing the reduced form for each number from 1 to MAX and to maintain the count array.