Ctrl K

Product of Distinct Primes

Hard
You get an integer array values. A pick is a set of array elements chosen by position; two picks are different when their sets of chosen positions differ. A pick is valid when the product of its elements equals a product of one or more **distinct** primes. For example, 6 = 2·3 and 7 qualify, while 4 = 2·2 and 1 do not. Return the number of valid picks modulo 10⁹ + 7.
Example 1
values=[1, 6, 4, 5] → 6

The valid picks are {6}, {5}, {6, 5} (product 30 = 2·3·5), and the same three with the 1 added: 6 in total. The 4 can never be used, since 4 = 2·2.

Example 2
values=[3, 3, 10] → 5

{3} can be taken two ways (either 3), {10} one way, {3, 10} two ways. Both 3s together give 9 = 3·3, which does not count. 5 in total.

Example 3
values=[1, 1, 9] → 0

A product of ones is 1, which has no prime factors, and 9 = 3·3. There are no valid picks.

Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 30
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Groups With the Same Average HardSquare-Sum Chains HardBump Numbers to Fit the Divisors HardFewest Lines Through the Dots MediumProducts Free of Squares Medium
loading Python…
Input
values=[1, 6, 4, 5]
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 94 tests on the server, hidden ones included.