<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>

from collections import Counter
class Solution:
    def sumFourDivisors(self, nums: List[int]) -> int:
        # Time complexity: O(m*log log m + n*log m), space complexity:
        # O(m), where n = len(nums) and m = max(nums).
        ctr = Counter(nums)
        vmax = max(nums)
        primes = [False, False] + [True]*(vmax - 1)
        for i in range(2, vmax):
            if i**2 > vmax: break
            if primes[i]:
                k = 2
                while i*k <= vmax:
                    primes[i*k] = False
                    k += 1
        prime_nums = [i for i,x in enumerate(primes) if x]
        
        ans = 0
        for k,v in ctr.items():
            if k < 2 or primes[k]: continue
            # Find the smallest prime factor of k.
            i = 0
            while k % prime_nums[i] != 0: i += 1
            if k == prime_nums[i]**3:
            # k has four divisors if it is the cube of a prime
            # number.
                ans += (1 + prime_nums[i] + prime_nums[i]**2 + k)*v
            else:
            # Or if it is the product of two distinct prime numbers.
                y = k // prime_nums[i]
                if primes[y] and y != prime_nums[i]:
                    ans += (1 + prime_nums[i] + y + k)*v
        return ans
19700