📜  构造从 N 和 A[i] 开始的最长数组作为 A[i+1] 的倍数

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

构造从 N 和 A[i] 开始的最长数组作为 A[i+1] 的倍数

给定一个整数N ,任务是构造可能的最长数组A[],使得以下条件成立:

  • A[0] = N。
  • 没有两个相邻的元素应该相等。
  • 对于所有i (0 < i < 数组长度),使得 A[i] 可以被 A[i + 1] 整除

注意:如果有很多可能的序列,打印任何序列。

例子:

方法:解决这个问题并最大化序列长度的直觉是:

请按照下图更好地理解。

插图:

以下是实施上述方法的步骤:

  • 运行一个循环直到N大于 1
    • 找出这个数的所有除数。
    • 将数的最大除数分配给N
  • 在最后一个 push 1 到结果数组。

以下是上述方法的实现:

C++
// C++ function to implement above approach
 
#include 
using namespace std;
 
// Function to find the maximum sequence
vector getMaximumSequence(int& N)
{
 
    // vector to store the sequence
    vector sequence;
 
    // Base case
    if (N == 1) {
        sequence.push_back(1);
        return sequence;
    }
    else {
 
        // Run the loop till the N is
        // greater than 1
        while (N > 1) {
 
            // Push the number in the
            // sequence
            sequence.push_back(N);
 
            // Declare maximum as 1 because
            // 1 is always the divisor
            // of the Number
            int maxx = 1;
 
            // Vector to track the
            // maximum divisors
            vector ds;
 
            ds.push_back(1);
 
            // Run a loop to find out all
            // the divisors except 1 and N
            for (int i = 2; i <= sqrt(N);
                 i++) {
 
                // If i is divisor of the
                // number then push_back it
                // in the ds vector
                if (N % i == 0) {
                    ds.push_back(i);
                    ds.push_back(N / i);
                }
            }
 
            // Assign N the maximum
            // divisors to get the
            // maximum sequence possible
            N = *max_element(ds.begin(),
                             ds.end());
        }
 
        // N will be equal to 1 thus,
        // push back it in the sequence
        // vector to complete the sequence
        sequence.push_back(N);
        return sequence;
    }
}
 
// Function to print sequence
void printSequence(vector& res)
{
    cout << res.size() << "\n";
    for (auto x : res) {
        cout << x << " ";
    }
}
 
// Driver Function
int main()
{
    int N = 8;
 
    // Function call
    vector res = getMaximumSequence(N);
    printSequence(res);
    return 0;
}


Javascript


Python3
# Python3 program to implement the above approach
 
 
# Function to find the maximum sequence
def getMaximumSequence(N):
    # vector to store the sequence
    sequence = []
    # Base case
    if N == 1:
        sequence.append(1)
        return sequence
    else:
        # Run the loop till the N is
        # greater than 1
        while N > 1:
            # push the number in the
            # sequence
            sequence.append(N)
            # Declare maximum as 1 because
            # 1 is always the divisor
            # of the Number
            maxx = 1
            # Vector to track the
            # maximum divisors
            ds = []
            ds.append(1)
            # Run a loop to find out all
            # the divisors
            for i in range(2, 1 + int(N ** 0.5)):
                # If i is divisor of the
                # number then push_back it
                # in the ds vector
                if N % i == 0:
                    ds.append(i)
                    ds.append(N // i)
            # Assign N the maximum
            # divisors to get the
            # maximum sequence possible
            N = max(ds)
        # N will be equal to 1 thus,
        # push back it in the sequence
        # vector to complete the sequence
        sequence.append(N)
        return sequence
 
# function to pring the sequence
def printSequence(res):
    print(len(res))
    print(" ".join(list(map(str, res))))
 
 
# Driver Code
N = 8
 
# Function Call
res = getMaximumSequence(N)
printSequence(res)
 
# This code is contributed by phasing17


输出
4
8 4 2 1 

时间复杂度: O(log 2 N * Sqrt(M))
辅助空间: O(log 2 N * M),其中 M 是除数,logN 是循环运行的次数