// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check number has distinct prime
bool checkDistinctPrime(int n)
{
int original = n;
int product = 1;
// While N has factors of two
if (n % 2 == 0) {
product *= 2;
while (n % 2 == 0) {
n /= 2;
}
}
// Traversing till sqrt(N)
for (int i = 3; i <= sqrt(n); i += 2) {
// If N has a factor of i
if (n % i == 0) {
product = product * i;
// While N has a factor of i
while (n % i == 0) {
n /= i;
}
}
}
// Covering case, N is Prime
if (n > 2) {
product = product * n;
}
return product == original;
}
// Function to check whether num can be added to the subset
bool check(int pos, vector<int>& subset,
vector<int>& unique)
{
for (int num : subset) {
if (__gcd(num, unique[pos]) != 1) {
return false;
}
}
return true;
}
// Recursive Function to count subset
int countPrime(int pos, vector<int> currSubset,
vector<int>& unique,
map<int, int>& frequency)
{
// Base Case
if (pos == unique.size()) {
// If currSubset is empty
if (currSubset.empty()) {
return 0;
}
int count = 1;
for (int element : currSubset) {
count *= frequency[element];
}
return count;
}
int ans = 0;
// If Unique[pos] can be added to the Subset
if (check(pos, currSubset, unique)) {
ans += countPrime(pos + 1, currSubset, unique,
frequency);
currSubset.push_back(unique[pos]);
ans += countPrime(pos + 1, currSubset, unique,
frequency);
}
else {
ans += countPrime(pos + 1, currSubset, unique,
frequency);
}
return ans;
}
// Function to count the subsets
int countSubsets(vector<int>& arr, int N)
{
// Initialize unique
set<int> uniqueSet;
for (int element : arr) {
// Check it is a product of distinct primes
if (checkDistinctPrime(element)) {
uniqueSet.insert(element);
}
}
vector<int> unique(uniqueSet.begin(), uniqueSet.end());
// Count frequency of unique element
map<int, int> frequency;
for (int element : unique) {
frequency[element]
= count(arr.begin(), arr.end(), element);
}
// Function Call
int ans
= countPrime(0, vector<int>(), unique, frequency);
return ans;
}
// Driver Code
int main()
{
// Given Input
vector<int> arr = { 2, 4, 7, 10 };
int N = arr.size();
// Function Call
int ans = countSubsets(arr, N);
cout << ans << endl;
return 0;
}