📜  超级丑数(给定集合中主要因子的数字)

📅  最后修改于: 2021-04-29 02:26:27             🧑  作者: Mango

超丑数是正数,其所有素数都在给定的素数列表中。给定数字n,任务是找到第n个“超级丑陋”数字。
可以假设给定的素数集已排序。同样,按照惯例,第一个超级丑陋数字是1。
例子:

Input  : primes[] = [2, 5]
         n = 5
Output : 8
Super Ugly numbers with given prime factors 
are 1, 2, 4, 5, 8, ...
Fifth Super Ugly number is 8

Input  : primes[] = [2, 3, 5]
         n = 50
Output : 243

Input : primes[] = [3, 5, 7, 11, 13]
        n = 9
Output: 21

在上一篇文章中,我们讨论了有关丑数的问题。这个问题基本上是Ugly Numbers的扩展。
解决此问题的简单方法是:如果所有素数均位于给定的素数集中,则意味着数字为Super Ugly,则从1开始逐个选择每个数字并找到其所有素数因子。重复此过程,直到获得第n个“超级丑陋编号”。
此问题的有效解决方案类似于“丑数”的“方法2”。这是算法:

  1. k为给定素数数组的大小。
  2. 声明一组超级丑陋的数字。
  3. 将第一个丑陋的数字(始终为1)插入到集合中。
  4. 初始化数组multiple_of [k]的大小为k为0这个数组的每个元素是迭代器在素数[k]的数组对应的素。
  5. 用primes [k]初始化nextMultipe [k]数组。该数组的行为类似于给定primes [k]数组中每个质数的下一个多个变量,即; nextMultiple [i] =素数[i] *丑陋的[++ multiple_of [i]]。
  6. 现在循环,直到丑陋的集合中有n个元素为止。
    一种)。在nextMultiple []数组的当前质数倍数中找到最小值,并将其插入丑陋的数字集中。
    b)。然后找到当前最小值是素数的倍数。
    C)。将迭代器增加1,即; ++ multiple_Of [i],用于当前选定质数的下一个倍数,并为其更新nextMultiple。

以下是上述步骤的实现。

CPP
// C++ program to find n'th Super Ugly number
#include
using namespace std;
 
// Function to get the nth super ugly number
// primes[]       --> given list of primes f size k
// ugly           --> set which holds all super ugly
//                    numbers from 1 to n
// k              --> Size of prime[]
int superUgly(int n, int primes[], int k)
{
    // nextMultiple holds multiples of given primes
    vector nextMultiple(primes, primes+k);
 
    // To store iterators of all primes
    int multiple_Of[k];
    memset(multiple_Of, 0, sizeof(multiple_Of));
 
    // Create a set to store super ugly numbers and
    // store first Super ugly number
    set ugly;
    ugly.insert(1);
 
    // loop until there are total n Super ugly numbers
    // in set
    while (ugly.size() != n)
    {
        // Find minimum element among all current
        // multiples of given prime
        int next_ugly_no = *min_element(nextMultiple.begin(),
                                    nextMultiple.end());
 
        // insert this super ugly number in set
        ugly.insert(next_ugly_no);
 
        // loop to find current minimum is multiple
        // of which prime
        for (int j=0; j  dp[++index[j]]
                set::iterator it = ugly.begin();
                for (int i=1; i<=multiple_Of[j]; i++)
                    it++;
 
                nextMultiple[j] = primes[j] * (*it);
                break;
            }
        }
    }
 
    // n'th super ugly number
    set::iterator it = ugly.end();
    it--;
    return *it;
}
 
/* Driver program to test above functions */
int main()
{
    int primes[] = {2,  5};
    int k = sizeof(primes)/sizeof(primes[0]);
    int n = 5;
    cout << superUgly(n, primes, k);
    return 0;
}


CPP
// C++ program for super ugly number
#include
using namespace std;
//function will return the nth super ugly number
int ugly(int a[], int size, int n){
     
    //n cannot be negative hence return -1 if n is 0 or -ve
    if(n <= 0)
        return -1;
  
    if(n == 1)
        return 1;
     
    // Declare a min heap priority queue
    priority_queue, greater> pq;
     
    // Push all the array elements to priority queue
    for(int i = 0; i < size; i++){
        pq.push(a[i]);
    }
     
    // once count = n we return no
    int count = 1, no;
     
    while(count < n){
        // Get the minimum value from priority_queue
        no = pq.top();
        pq.pop();
         
        // If top of pq is no then don't increment count. This to avoid duplicate counting of same no.
        if(no != pq.top())
        {
            count++;
         
            //Push all the multiples of no. to priority_queue
            for(int i = 0; i < size; i++){
                pq.push(no * a[i]);
            //    cnt+=1;
        }
        }
    }
    // Return nth super ugly number
    return no;
}
 
/* Driver program to test above functions */
int main(){
    int a[3] = {2, 3,5};
    int size = sizeof(a) / sizeof(a[0]);
    cout << ugly(a, size, 10)<


Python3
# Python3 program for super ugly number
 
# function will return the nth super ugly number
def ugly(a, size, n):
 
    # n cannot be negative hence return -1 if n is 0 or -ve
    if(n <= 0):
        return -1
    if(n == 1):
        return 1
 
    # Declare a min heap priority queue
    pq = []
 
    # Push all the array elements to priority queue
    for i  in range(size):
        pq.append(a[i])
 
    # once count = n we return no
    count = 1
    no = 0
    pq = sorted(pq)
 
    while(count < n):
        # sorted(pq)
        # Get the minimum value from priority_queue
        no = pq[0]
        del pq[0]
 
 
        # If top of pq is no then don't increment count.
        # This to avoid duplicate counting of same no.
        if(no != pq[0]):
            count += 1
 
            # Push all the multiples of no. to priority_queue
            for i in range(size):
                pq.append(no * a[i])
            #   cnt+=1
        pq = sorted(pq)
    # Return nth super ugly number
    return no
 
# /* Driver program to test above functions */
if __name__ == '__main__':
    a = [2, 3,5]
    size = len(a)
    print(ugly(a, size, 1000))
 
    # This code is contributed by mohit kumar 29.


输出:

8

CPP

// C++ program for super ugly number
#include
using namespace std;
//function will return the nth super ugly number
int ugly(int a[], int size, int n){
     
    //n cannot be negative hence return -1 if n is 0 or -ve
    if(n <= 0)
        return -1;
  
    if(n == 1)
        return 1;
     
    // Declare a min heap priority queue
    priority_queue, greater> pq;
     
    // Push all the array elements to priority queue
    for(int i = 0; i < size; i++){
        pq.push(a[i]);
    }
     
    // once count = n we return no
    int count = 1, no;
     
    while(count < n){
        // Get the minimum value from priority_queue
        no = pq.top();
        pq.pop();
         
        // If top of pq is no then don't increment count. This to avoid duplicate counting of same no.
        if(no != pq.top())
        {
            count++;
         
            //Push all the multiples of no. to priority_queue
            for(int i = 0; i < size; i++){
                pq.push(no * a[i]);
            //    cnt+=1;
        }
        }
    }
    // Return nth super ugly number
    return no;
}
 
/* Driver program to test above functions */
int main(){
    int a[3] = {2, 3,5};
    int size = sizeof(a) / sizeof(a[0]);
    cout << ugly(a, size, 10)<

Python3

# Python3 program for super ugly number
 
# function will return the nth super ugly number
def ugly(a, size, n):
 
    # n cannot be negative hence return -1 if n is 0 or -ve
    if(n <= 0):
        return -1
    if(n == 1):
        return 1
 
    # Declare a min heap priority queue
    pq = []
 
    # Push all the array elements to priority queue
    for i  in range(size):
        pq.append(a[i])
 
    # once count = n we return no
    count = 1
    no = 0
    pq = sorted(pq)
 
    while(count < n):
        # sorted(pq)
        # Get the minimum value from priority_queue
        no = pq[0]
        del pq[0]
 
 
        # If top of pq is no then don't increment count.
        # This to avoid duplicate counting of same no.
        if(no != pq[0]):
            count += 1
 
            # Push all the multiples of no. to priority_queue
            for i in range(size):
                pq.append(no * a[i])
            #   cnt+=1
        pq = sorted(pq)
    # Return nth super ugly number
    return no
 
# /* Driver program to test above functions */
if __name__ == '__main__':
    a = [2, 3,5]
    size = len(a)
    print(ugly(a, size, 1000))
 
    # This code is contributed by mohit kumar 29.

输出:

51200000