【題目描述】
Write a program to find the nth super ugly number.
Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size?k. For example,?[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32]?is the sequence of the first 12 super ugly numbers given primes =?[2, 7, 13, 19]?of size?4.
?Notice
1?is a super ugly number for any given primes.
The given numbers in primes are in ascending order.
0 < k ≤ 100, 0 < n ≤ 10^6, 0 < primes[i] < 1000
寫一個程序來找第?n?個超級丑數。
超級丑數的定義是正整數并且所有的質數因子都在所給定的一個大小為?k?的質數集合內。
比如給你 4 個質數的集合?[2, 7, 13, 19], 那么?[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32]?是前 12 個超級丑數。
?注意事項
1?永遠都是超級丑數不管給的質數集合是什么。
給你的質數集合已經按照升序排列。
0 <?k?≤ 100, 0 <?n?≤ 10^6, 0 <?primes[i] < 1000
【題目鏈接】
www.lintcode.com/en/problem/super-ugly-number/
【題目解析】
這道題讓我們求超級丑陋數,是之前Ugly Number的延伸,質數集合可以任意給定,這就增加了難度。
由于我們不知道質數的個數,我們可以用一個idx數組來保存當前的位置,然后我們從每個子鏈中取出一個數,找出其中最小值,然后更新idx數組對應位置,注意有可能最小值不止一個,要更新所有最小值的位置。
【參考答案】