📌  相关文章
📜  生成一个具有乘积 N 的序列,使得对于每对索引 (i, j) 和 i < j,arr[j] 可以被 arr[i] 整除

📅  最后修改于: 2022-05-13 01:56:10.032000             🧑  作者: Mango

生成具有乘积 N 的序列,使得对于每对索引 (i, j) 和 i < j,arr[j] 可以被 arr[i] 整除

给定一个正整数N ,任务是生成一个序列,说arr[]的最大长度,其中所有元素至少为 2 ,使得序列中所有数字的乘积为N并且对于任何一对索引(i, j)并且i < jarr[j]可以被arr[i]整除。

例子:

方法:给定的问题可以通过使用素数分解的概念来解决,其思想是找到出现频率为N的最大素数,可以表示为素数的乘积:

请按照以下步骤解决问题:

  • 初始化一个 Map,比如M ,它将所有素数存储为键,将它们的幂存储为值。例如,对于值2 3 ,映射M存储为M[2] = 3
  • 选择具有最大功率因数的素数,并将该功率存储在变量中,例如ans ,并将该素数存储在变量中,例如P
  • 如果ans的值小于2 ,那么结果数组的大小将是1并且数组元素将等于N
  • 否则,打印ans的值作为最大长度,并打印最终序列,打印P (ans – 1)次的值,然后在末尾打印 ( N/2 ans )

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate the prime
// factors of N with their count
vector > primeFactor(
    int N)
{
    // Initialize a vector, say v
    vector > v;
 
    // Initialize the count
    int count = 0;
 
    // Count the number of divisors
    while (!(N % 2)) {
 
        // Divide the value of N by 2
        N >>= 1;
        count++;
    }
 
    // For factor 2 divides it
    if (count)
        v.push_back({ 2, count });
 
    // Find all prime factors
    for (int i = 3;
         i <= sqrt(N); i += 2) {
 
        // Count their frequency
        count = 0;
        while (N % i == 0) {
            count++;
            N = N / i;
        }
 
        // Push it to the vector
        if (count) {
            v.push_back({ i, count });
        }
    }
 
    // Push N if it is not 1
    if (N > 2)
        v.push_back({ N, 1 });
 
    return v;
}
 
// Function to print the array that
// have the maximum size
void printAnswer(int n)
{
    // Stores the all prime factor
    // and their powers
    vector > v
        = primeFactor(n);
 
    int maxi_size = 0, prime_factor = 0;
 
    // Traverse the vector and find
    // the maximum power of prime
    // factor
    for (int i = 0; i < v.size(); i++) {
 
        if (maxi_size < v[i].second) {
            maxi_size = v[i].second;
            prime_factor = v[i].first;
        }
    }
 
    // If max size is less than 2
    if (maxi_size < 2) {
        cout << 1 << ' ' << n;
    }
 
    // Otherwise
    else {
 
        int product = 1;
 
        // Print the maximum size
        // of sequence
        cout << maxi_size << endl;
 
        // Print the final sequence
        for (int i = 0;
             i < maxi_size - 1; i++) {
 
            // Print the prime factor
            cout << prime_factor << " ";
            product *= prime_factor;
        }
 
        // Print the last value of
        // the sequence
        cout << (n / product);
    }
}
 
// Driver Code
int main()
{
    int N = 360;
    printAnswer(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
    static class pair
    {
        int first, second;
        public pair(int first, int second) 
        {
            this.first = first;
            this.second = second;
        }   
    }
// Function to calculate the prime
// factors of N with their count
static Vector primeFactor(
    int N)
{
   
    // Initialize a vector, say v
    Vector v = new Vector<>();
 
    // Initialize the count
    int count = 0;
 
    // Count the number of divisors
    while ((N % 2)==0) {
 
        // Divide the value of N by 2
        N >>= 1;
        count++;
    }
 
    // For factor 2 divides it
    if (count!=0)
        v.add(new pair( 2, count ));
 
    // Find all prime factors
    for (int i = 3;
         i <= Math.sqrt(N); i += 2) {
 
        // Count their frequency
        count = 0;
        while (N % i == 0) {
            count++;
            N = N / i;
        }
 
        // Push it to the vector
        if (count!=0) {
            v.add(new pair( i, count ));
        }
    }
 
    // Push N if it is not 1
    if (N > 2)
        v.add(new pair( N, 1 ));
 
    return v;
}
 
// Function to print the array that
// have the maximum size
static void printAnswer(int n)
{
    // Stores the all prime factor
    // and their powers
    Vector v
        = primeFactor(n);
 
    int maxi_size = 0, prime_factor = 0;
 
    // Traverse the vector and find
    // the maximum power of prime
    // factor
    for (int i = 0; i < v.size(); i++) {
 
        if (maxi_size < v.get(i).second) {
            maxi_size = v.get(i).second;
            prime_factor = v.get(i).first;
        }
    }
 
    // If max size is less than 2
    if (maxi_size < 2) {
        System.out.print(1 << ' ');
    }
 
    // Otherwise
    else {
 
        int product = 1;
 
        // Print the maximum size
        // of sequence
        System.out.print(maxi_size +"\n");
 
        // Print the final sequence
        for (int i = 0;
             i < maxi_size - 1; i++) {
 
            // Print the prime factor
            System.out.print(prime_factor+ " ");
            product *= prime_factor;
        }
 
        // Print the last value of
        // the sequence
        System.out.print((n / product));
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 360;
    printAnswer(N);
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
from math import sqrt
 
# Function to calculate the prime
# factors of N with their count
def primeFactor(N):
     
    # Initialize a vector, say v
    v = []
 
    # Initialize the count
    count = 0
 
    # Count the number of divisors
    while ((N % 2) == 0):
         
        # Divide the value of N by 2
        N >>= 1
        count += 1
 
    # For factor 2 divides it
    if (count):
        v.append([2, count])
 
    # Find all prime factors
    for i in range(3, int(sqrt(N)) + 1, 2):
         
        # Count their frequency
        count = 0
        while (N % i == 0):
            count += 1
            N = N / i
 
        # Push it to the vector
        if (count):
            v.append([i, count])
 
    # Push N if it is not 1
    if (N > 2):
        v.append([N, 1])
 
    return v
 
# Function to print the array that
# have the maximum size
def printAnswer(n):
     
    # Stores the all prime factor
    # and their powers
    v = primeFactor(n)
 
    maxi_size = 0
    prime_factor = 0
 
    # Traverse the vector and find
    # the maximum power of prime
    # factor
    for i in range(len(v)):
        if (maxi_size < v[i][1]):
            maxi_size = v[i][1]
            prime_factor = v[i][0]
 
    # If max size is less than 2
    if (maxi_size < 2):
        print(1, n)
 
    # Otherwise
    else:
        product = 1
 
        # Print the maximum size
        # of sequence
        print(maxi_size)
 
        # Print the final sequence
        for i in range(maxi_size - 1):
             
            # Print the prime factor
            print(prime_factor, end = " ")
            product *= prime_factor
 
        # Print the last value of
        # the sequence
        print(n // product)
 
# Driver Code
if __name__ == '__main__':
     
    N = 360
     
    printAnswer(N)
     
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
    class pair
    {
        public int first;
        public int second;
        public pair(int first, int second) 
        {
            this.first = first;
            this.second = second;
        }   
    }
   
// Function to calculate the prime
// factors of N with their count
static List primeFactor(
    int N)
{
   
    // Initialize a vector, say v
    List v = new List();
 
    // Initialize the count
    int count = 0;
 
    // Count the number of divisors
    while ((N % 2)==0) {
 
        // Divide the value of N by 2
        N >>= 1;
        count++;
    }
 
    // For factor 2 divides it
    if (count!=0)
        v.Add(new pair( 2, count ));
 
    // Find all prime factors
    for (int i = 3;
         i <= Math.Sqrt(N); i += 2) {
 
        // Count their frequency
        count = 0;
        while (N % i == 0) {
            count++;
            N = N / i;
        }
 
        // Push it to the vector
        if (count!=0) {
            v.Add(new pair( i, count ));
        }
    }
 
    // Push N if it is not 1
    if (N > 2)
        v.Add(new pair( N, 1 ));
 
    return v;
}
 
// Function to print the array that
// have the maximum size
static void printAnswer(int n)
{
   
    // Stores the all prime factor
    // and their powers
    List v
        = primeFactor(n);
 
    int maxi_size = 0, prime_factor = 0;
 
    // Traverse the vector and find
    // the maximum power of prime
    // factor
    for (int i = 0; i < v.Count; i++) {
 
        if (maxi_size < v[i].second) {
            maxi_size = v[i].second;
            prime_factor = v[i].first;
        }
    }
 
    // If max size is less than 2
    if (maxi_size < 2) {
        Console.Write(1 << ' ');
    }
 
    // Otherwise
    else {
 
        int product = 1;
 
        // Print the maximum size
        // of sequence
        Console.Write(maxi_size +"\n");
 
        // Print the readonly sequence
        for (int i = 0;
             i < maxi_size - 1; i++) {
 
            // Print the prime factor
            Console.Write(prime_factor+ " ");
            product *= prime_factor;
        }
 
        // Print the last value of
        // the sequence
        Console.Write((n / product));
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 360;
    printAnswer(N);
 
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
3
2 2 90

时间复杂度: O(N 3/2 )
辅助空间: O(N)