1
// Finds the nth super ugly number given a list of prime numbers.5
int nthSuperUglyNumber(int n, vector<int> &primes) {8
int numPrimes = primes.size(); // Number of prime numbers9
vector<int> primeIndices(numPrimes,10
0); // Indices to track prime number multiples12
int superUgly[n]; // Array to store super ugly numbers13
// memset(superUgly, 0, sizeof(superUgly)); // Initialize the array14
// (commented out since it's unnecessary)15
superUgly[0] = 1; // First super ugly number is always 117
for (int i = 1; i < n; i++) {18
long minVal = INT_MAX; // Minimum value among the prime number multiples20
// Find the minimum value among the prime number multiples21
for (int j = 0; j < numPrimes; j++) {22
minVal = min(minVal, (long)primes[j] * superUgly[primeIndices[j]]);25
superUgly[i] = (int)minVal; // Store the minimum value as the next super ugly number27
// Increment the indices for prime number multiples that contribute to the29
for (int j = 0; j < numPrimes; j++) {30
if (minVal == (long)primes[j] * superUgly[primeIndices[j]]) {35
// cout<<superUgly[i]<<","; // Print the current super ugly number36
// (commented out for clarity)39
return superUgly[n - 1]; // Return the nth super ugly number