<p>Given an integer array <code>nums</code>, return <em>the sum of divisors of the integers in that array that have exactly four divisors</em>. If there is no such integer in the array, return <code>0</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [21,4,7]
<strong>Output:</strong> 32
<strong>Explanation:</strong> 
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [21,21]
<strong>Output:</strong> 64
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

static const int s_primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317};

class Solution {
private:
    static void PrimeFactorization(int num, std::unordered_map<int, int>& primePowerMap) {
        for (int i = 0; i < (sizeof(s_primes) / sizeof(s_primes[0])); ++i) {
            int prime = s_primes[i];
            while ((num % prime) == 0) {
                ++primePowerMap[prime];
                num /= prime;
            }
        }
        if (num > 1) {
            primePowerMap[num] = 1;
        }     
    }

    // Returns power of a number
    static long long Power(int n, int p) {
        long long ans = 1;
        for (int i = 0; i < p; ++i) {
            ans *= n;
        }
        return ans;
    }

    static int NumDivisors(const std::unordered_map<int, int>& primePowerMap) {
        int nDivs = 1;
        for (auto mapPair : primePowerMap) {
            int prime = mapPair.first;
            int p = mapPair.second;
            nDivs *= (p+1);
        }
        return nDivs;
    }

    static int SumDivisors(const std::unordered_map<int, int>& primePowerMap) {
        long long sumDivs = 1;
        for (auto mapPair : primePowerMap) {
            int prime = mapPair.first;
            int p = mapPair.second;
            sumDivs *= ((Power(prime, p+1)-1) / (prime-1));
        }
        return sumDivs;
    }    

public:
    long long sumFourDivisors(const std::vector<int>& nums) {
        long long sum = 0;
        int sumDivs = 0;
        for (auto num : nums) {
            std::unordered_map<int, int> primePowerMap;
            PrimeFactorization(num, primePowerMap);

            /*std::cout << "num : " << num << std::endl;
            for (auto mapPair : primePowerMap) {
                std::cout << mapPair.first << "^" << mapPair.second << " | ";
            }
            std::cout << std::endl;*/

            int numDivs = NumDivisors(primePowerMap);
            //std::cout << num << " " << numDivs << " " << sumDivs << std::endl;
            if (numDivs == 4) {
                sum += SumDivisors(primePowerMap);
            }
        }
        return sum;
    }
};
33900