101 Logo
onenoughtone

Problem Statement

Prime Factor Sequence

You're a mathematician studying number sequences with specific prime factorizations. You're interested in a special type of positive integers called "super ugly numbers."

A super ugly number is a positive integer whose prime factors are limited to a specific set of prime numbers. In other words, a super ugly number can only be divided by the prime numbers in a given list.

Given an integer n and an array of prime numbers primes, your task is to find the nth super ugly number in the sequence.

Note:

  • 1 is considered a super ugly number for any given set of primes.
  • The sequence of super ugly numbers is sorted in ascending order.
  • The prime numbers in the input array are distinct and sorted in ascending order.

Examples

Example 1:

Input: n = 12, primes = [2,7,13,19]
Output: 32
Explanation: The sequence of the first 12 super ugly numbers is [1,2,4,7,8,13,14,16,19,26,28,32]. Note that 32 = 2^5, which only uses the prime factor 2 from the given list.

Example 2:

Input: n = 1, primes = [2,3,5]
Output: 1
Explanation: The first super ugly number is 1, which has no prime factors.

Constraints

  • 1 <= n <= 10^5
  • 1 <= primes.length <= 100
  • 2 <= primes[i] <= 1000
  • primes[i] is guaranteed to be a prime number
  • All the values of primes are unique and sorted in ascending order
  • The nth super ugly number is guaranteed to fit in a 32-bit signed integer

Problem Breakdown

To solve this problem, we need to:

  1. The sequence starts with 1, which is considered a super ugly number by definition
  2. Each subsequent super ugly number is generated by multiplying an existing super ugly number by one of the prime factors
  3. The key is to efficiently generate the sequence in ascending order
  4. This problem is similar to the 'Ugly Number II' problem, but with a variable set of prime factors
  5. Dynamic programming or a priority queue (min heap) can be used to efficiently generate the sequence
  6. Avoiding duplicate numbers in the sequence is important
ProblemSolutionCode
101 Logo
onenoughtone