0313.cpp (640B)
1 class Solution { 2 public: 3 int nthSuperUglyNumber(int n, const vector<int> &primes) const { 4 using type_t = tuple<long long, int, int>; 5 priority_queue<type_t, vector<type_t>, greater<>> pq; 6 7 for (int i = 0; i < size(primes); i++) { 8 pq.emplace(primes[i], primes[i], 0); 9 } 10 11 static int nums[100002] = {1, 0}; 12 for (int i = 1; i < n;) { 13 const auto [num, prime, idx] = pq.top(); 14 pq.pop(); 15 if (num != nums[i - 1]) nums[i++] = num; 16 pq.emplace(1ll * prime * nums[idx + 1], prime, idx + 1); 17 } 18 19 return nums[n - 1]; 20 } 21 };