<p>You are given a <strong>1</strong><strong>-indexed</strong> array <code>nums</code>. Your task is to select a <strong>complete subset</strong> from <code>nums</code> where every pair of selected indices multiplied is a <span data-keyword="perfect-square">perfect square,</span>. i. e. if you select <code>a<sub>i</sub></code> and <code>a<sub>j</sub></code>, <code>i * j</code> must be a perfect square.</p>

<p>Return the <em>sum</em> of the complete subset with the <em>maximum sum</em>.</p>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [8,7,3,5,7,2,4,9]</span></p>

<p><strong>Output:</strong> <span class="example-io">16</span></p>

<p><strong>Explanation:</strong></p>

<p>We select elements at indices 2 and 8 and 2<code>&nbsp;* 8</code>&nbsp;is a perfect square.</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [8,10,3,8,1,13,7,9,4]</span></p>

<p><strong>Output:</strong> <span class="example-io">20</span></p>

<p><strong>Explanation:</strong></p>

<p>We select elements at indices 1, 4, and 9. <code>1 * 4</code>, <code>1 * 9</code>, <code>4 * 9</code> are perfect squares.</p>
</div>

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

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

class Solution:
    def maximumSum(self, nums: List[int]) -> int:

        # method #3: 
        # 0. sieve of eratotheses through all of the numbers up to nums.length
        #   - maintain (is_prime, prime_dividing it if not prime)
        # 1. gives number of primes --> hash size
        # 2. use DP to compute the parity hash of each index
        # 2. bucket each index by it's prime parity
        # 3. compute sum over each grouped hash and return maximum

        # allocate storage for prime sieve for if is_prime and prime_divisor

        max_idx = len(nums)
        is_prime_list = [True] * max_idx
        prime_divisor_list = [0] * max_idx

        # 1 is not a prime
        is_prime_list[0] = False

        # O(N^2)
        # go through each number, 1...length, 
        for zb_idx in range(1, max_idx):
            num = zb_idx + 1
            
            if not is_prime_list[zb_idx]:
                # if no longer prime, skip
                continue
            else:
                # if prime, go through and remove primes from all multiples
                # increase to set composite numbers
                prime = num
                num += prime
                while num <= max_idx:
                    is_prime_list[num - 1] = False
                    # set divisor if necessary
                    prime_divisor_list[num - 1] = prime
                    num += prime

        # get the number of primes
        num_primes = sum(is_prime_list)

        # use dp to compute the prime hashes
        memo = {1: 0}

        # initialize each prime as a given hash
        prime_idx = 1
        for idx, is_prime in enumerate(is_prime_list):
            if is_prime:
                prime = idx + 1
                memo[prime] = 1 << prime_idx
                prime_idx += 1

        # then iterate through each composite number and compute hash 
        # prime hash and smaller composite hash
        for idx in range(1, len(is_prime_list)):
            is_prime = is_prime_list[idx]
            divisor = prime_divisor_list[idx]

            if not is_prime:
                composite = idx + 1
                memo[composite] = memo[composite // divisor] ^ memo[divisor]

        # then compute sums by hash and return largest
        sums = {}
        for ob_idx, parity_bits in memo.items():
            if parity_bits not in sums:
                sums[parity_bits] = 0
            sums[parity_bits] += nums[ob_idx - 1]

        return max([s for s in sums.values()])

        
        
344