<p>You are given two positive integer arrays <code>nums</code> and <code>numsDivide</code>. You can delete any number of elements from <code>nums</code>.</p>

<p>Return <em>the <strong>minimum</strong> number of deletions such that the <strong>smallest</strong> element in </em><code>nums</code><em> <strong>divides</strong> all the elements of </em><code>numsDivide</code>. If this is not possible, return <code>-1</code>.</p>

<p>Note that an integer <code>x</code> divides <code>y</code> if <code>y % x == 0</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [2,3,2,4,3], numsDivide = [9,6,9,3,15]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
The smallest element in [2,3,2,4,3] is 2, which does not divide all the elements of numsDivide.
We use 2 deletions to delete the elements in nums that are equal to 2 which makes nums = [3,4,3].
The smallest element in [3,4,3] is 3, which divides all the elements of numsDivide.
It can be shown that 2 is the minimum number of deletions needed.
</pre>

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

<pre>
<strong>Input:</strong> nums = [4,3,6], numsDivide = [8,2,6,10]
<strong>Output:</strong> -1
<strong>Explanation:</strong> 
We want the smallest element in nums to divide all the elements of numsDivide.
There is no way to delete elements from nums to allow this.</pre>

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

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

class Solution:
    # Returns all primes <= x
    def primeSieve(self, x:int) -> Set[int]:
        primes = [True]*(x + 1)
        primes[0] = False
        primes[1] = False
        for i in range(4, x + 1, 2):
            primes[i] = False
        for i in range(3, x // 3 + 1):
            v = 3*i
            while v < x:
                primes[v] = False
                v += 2*i
        return set([(inf, p) for p in filter(lambda x: primes[x], range(x + 1))])

    def minOperations(self, nums: List[int], numsDivide: List[int]) -> int:
        # sort nums and find max value
        # remove dups from numsDivide
        # find all prime factors of numsDivide[0] <= max[nums]
        # prime sieve has at most sqrt(10^9) elements
        # if no primes in sieve divide numsDivide[0] it must be prime
        # then keep track of list of primes that divide each element of numsDivide
        # find first element of nums that divides product of list of primes
        nums.sort()
        if nums[0] == 1:
            return 0
        numsDivide = list(set(numsDivide))
        maxNum = max(numsDivide)
        primes = self.primeSieve(int(maxNum**0.5))
        #filter primes so it only contains primes that divide every element of numsDivide
        for num in numsDivide:
            #print(primes)
            newPrimes = []
            for (count, prime) in primes:
                numdiv, nummod = divmod(num, prime)
                newCount = 0
                while nummod == 0 and newCount < count:
                    newCount += 1
                    num = numdiv
                    numdiv, nummod = divmod(num, prime)
                if newCount > 0:
                    newPrimes.append((newCount, prime))
            primes = newPrimes
        primeProduct = 1
        for (count, prime) in primes:
            primeProduct *= prime**count
        if len(numsDivide) == 1 and primes == []:
            primeProduct = maxNum
        prev = -1
        for i, num in enumerate(nums):
            if num == prev:
                continue
            prev = num
            if primeProduct % num == 0:
                return i
            if num > primeProduct:
                break
        return -1
798