Suppose arr = [1, 3, 9, 5]
Valid subsets are as follows:
[3], Product = 3.
[1, 3], Product = 1 * 3.
[5], Product = 5.
[1, 5], Product = 1 * 5.
[3, 5], Product = 3 * 5.
[1, 3, 5], Product = 1 * 3 * 5.
All of the above subsets’ products can be represented only with unit powers of prime and have at least one prime in their product. Hence they are valid. As we have six valid subsets, our output will be 6.
The first line of the input contains a single integer ‘T’ representing the no. of test cases.
The first line of each test case contains a single integer ‘N’, denoting the size of ‘Arr’.
The second line of each test case contains ‘N’ space-separated integers, denoting the elements of ‘Arr’.
For each test case, print a single integer value denoting the number of valid subsets modulo (10^9+7).
Print a separate line for each test case.
You are not required to print anything; it has already been taken care of. Just implement the function and return the answer.
1 ≤ T ≤ 1000
1 ≤ N ≤ 10^5
1 ≤ Arr[i] ≤ 20
1 ≤ ΣN ≤ 2 * 10^5
Time limit: 1 Sec
We can recursively generate all the possible subsets of Arr and for each subset we can check whether its product can be expressed as product of unit powers of one or more primes. Implementation details will be as per the following algorithm.
Check Function
Function arguments - integer array ‘subset’ to check for validity.
Returns boolean value whether the given subset is valid.
Subsets Function
Function arguments - integer array ‘arr’, integer array ‘subset’ to maintain current subset, 64Bit integer ‘ans, integer value ‘ind’
Generates all subsets of arr and check them for validity.
Given Function
We have a very low number of possible Arr[i] values as constraints are very low. Moreover, we can completely ignore some values like 4, 9, 12, etc., which contains multiple power of a single prime, as they can never be part of a valid subset. In total, after these removals, we are left with less than 14 values for possible values of Arr[i], which can be included in a valid subset. We will consider all of these (around 2^20) candidate subsets and check them for validity. If a subset is valid, its total number of occurrences as distinct subsets can be easily estimated by multiplying the frequency of occurrence of each value in ‘Arr’. To check whether a subset is valid or not efficiently, we can store each value’s prime factorization in a bitmask. The ith bit is set in the bitmask if it is divisible by the prime i. Implementation details are as per the following Algorithm.
Algorithm:
Factorize Function
Function arguments - integer value ‘M’ to be factorized.
Returns - Integer bitmask representing prime factorization of ‘M’.
Check Function
Function arguments - integer array ‘subset’ to check for validity, integer array ‘freq’ storing frequency values of every value, integer array ‘factors’ containing mask values for prime factorization, 64Bit integer ‘ans’ to which will store the actual answer.
Checks whether the given subset is valid. If the subset is valid then add the frequency of that subset in ‘ans’.
Subsets Function
Function arguments - integer array of valid options to construct subarrays, integer array ‘subset’ to maintain current subset, 64Bit integer ‘ans, integer array ‘freq’ to store frequency of each value, integer array ‘factors’ to store prime factorization masks for each value, integer value ‘ind’
Creates every possible subset of all valid options which can be included in a subset.
Given Function