📜  最长递增子序列的长度,以使两个相邻元素都不互质

📅  最后修改于: 2021-04-25 00:17:16             🧑  作者: Mango

给定大小为N的数组arr []。任务是从给定数组中找到最长子序列的长度,以使序列严格增加,并且没有两个相邻元素互质。

注意:给定数组中的元素严格按顺序增加(1 <= a [i] <= 10 5 )

例子:

方法:主要思想是使用动态编程的概念。让我们将dp [x]定义为最后一个元素为x的子序列长度的最大值,然后将d [i]定义为(x可以被i整除的dp [x]的最大值)。
我们应该按x的升序计算dp [x]。 dp [x]的值是(d [i]的最大值,其中i是x的除数)+1。在计算dp [x]之后,对于x的每个除数i,我们也应更新d [i] 。该算法适用于O(N * logN),因为从1到N的除数之和为O(N * logN)。

注意:有一个角壳。集合为{1}时,应输出1。

下面是上述方法的实现:

C++
// CPP program to find the length of the
// longest  increasing sub sequence from the given array
// such that no two adjacent elements are co prime
 
#include 
using namespace std;
#define N 100005
 
// Function to find the length of the
// longest  increasing sub sequence from the given array
// such that no two adjacent elements are co prime
int LIS(int a[], int n)
{
    // To store dp and d value
    int dp[N], d[N];
 
    // To store required answer
    int ans = 0;
 
    // For all elements in the array
    for (int i = 0; i < n; i++) {
        // Initially answer is one
        dp[a[i]] = 1;
 
        // For all it's divisors
        for (int j = 2; j * j <= a[i]; j++) {
            if (a[i] % j == 0) {
                // Update the dp value
                dp[a[i]] = max(dp[a[i]], dp[d[j]] + 1);
                dp[a[i]] = max(dp[a[i]], dp[d[a[i] / j]] + 1);
 
                // Update the divisor value
                d[j] = a[i];
                d[a[i] / j] = a[i];
            }
        }
 
        // Check for required answer
        ans = max(ans, dp[a[i]]);
 
        // Update divisor of a[i]
        d[a[i]] = a[i];
    }
 
    // Return reqired answer
    return ans;
}
 
// Driver code
int main()
{
    int a[] = { 1, 2, 3, 4, 5, 6 };
 
    int n = sizeof(a) / sizeof(a[0]);
 
    cout << LIS(a, n);
 
    return 0;
}


Java
// Java program to find the length
// of the longest increasing sub
// sequence from the given array
// such that no two adjacent
// elements are co prime
 
class GFG
{
    static int N=100005;
 
// Function to find the length of the
// longest increasing sub sequence
// from the given array such that
// no two adjacent elements are co prime
static int LIS(int a[], int n)
{
    // To store dp and d value
    int dp[]=new int[N], d[]=new int[N];
 
    // To store required answer
    int ans = 0;
 
    // For all elements in the array
    for (int i = 0; i < n; i++)
    {
        // Initially answer is one
        dp[a[i]] = 1;
 
        // For all it's divisors
        for (int j = 2; j * j <= a[i]; j++)
        {
            if (a[i] % j == 0)
            {
                // Update the dp value
                dp[a[i]] = Math.max(dp[a[i]], dp[d[j]] + 1);
                dp[a[i]] = Math.max(dp[a[i]], dp[d[a[i] / j]] + 1);
 
                // Update the divisor value
                d[j] = a[i];
                d[a[i] / j] = a[i];
            }
        }
 
        // Check for required answer
        ans = Math.max(ans, dp[a[i]]);
 
        // Update divisor of a[i]
        d[a[i]] = a[i];
    }
 
    // Return reqired answer
    return ans;
}
 
// Driver code
public static void main(String args[])
{
    int a[] = { 1, 2, 3, 4, 5, 6 };
 
    int n = a.length;
 
    System.out.print( LIS(a, n));
}
}
 
// This code is contributed by Arnab Kundu


Python3
# Python3 program to find the length of the
# longest increasing sub sequence from the
# given array such that no two adjacent
# elements are co prime
N = 100005
 
# Function to find the length of the
# longest increasing sub sequence from
# the given array such that no two
# adjacent elements are co prime
def LIS(a, n):
     
    # To store dp and d value
    dp = [0 for i in range(N)]
    d = [0 for i in range(N)]
 
    # To store required answer
    ans = 0
 
    # For all elements in the array
    for i in range(n):
         
        # Initially answer is one
        dp[a[i]] = 1
 
        # For all it's divisors
        for j in range(2, a[i]):
            if j * j > a[i]:
                break
            if (a[i] % j == 0):
                 
                # Update the dp value
                dp[a[i]] = max(dp[a[i]],
                               dp[d[j]] + 1)
                dp[a[i]] = max(dp[a[i]],
                               dp[d[a[i] // j]] + 1)
 
                # Update the divisor value
                d[j] = a[i]
                d[a[i] // j] = a[i]
 
        # Check for required answer
        ans = max(ans, dp[a[i]])
 
        # Update divisor of a[i]
        d[a[i]] = a[i]
 
    # Return reqired answer
    return ans
 
# Driver code
a = [1, 2, 3, 4, 5, 6]
 
n = len(a)
 
print(LIS(a, n))
 
# This code is contributed by mohit kumar


C#
// C# program to find the length
// of the longest increasing sub
// sequence from the given array
// such that no two adjacent
// elements are co prime
using System;
 
class GFG
{
    static int N = 100005;
 
    // Function to find the length of the
    // longest increasing sub sequence
    // from the given array such that
    // no two adjacent elements are co prime
    static int LIS(int []a, int n)
    {
        // To store dp and d value
        int []dp = new int[N];
        int []d = new int[N];
     
        // To store required answer
        int ans = 0;
     
        // For all elements in the array
        for (int i = 0; i < n; i++)
        {
            // Initially answer is one
            dp[a[i]] = 1;
     
            // For all it's divisors
            for (int j = 2; j * j <= a[i]; j++)
            {
                if (a[i] % j == 0)
                {
                    // Update the dp value
                    dp[a[i]] = Math.Max(dp[a[i]], dp[d[j]] + 1);
                    dp[a[i]] = Math.Max(dp[a[i]], dp[d[a[i] / j]] + 1);
     
                    // Update the divisor value
                    d[j] = a[i];
                    d[a[i] / j] = a[i];
                }
            }
     
            // Check for required answer
            ans = Math.Max(ans, dp[a[i]]);
     
            // Update divisor of a[i]
            d[a[i]] = a[i];
        }
     
        // Return reqired answer
        return ans;
    }
     
    // Driver code
    public static void Main()
    {
        int []a = { 1, 2, 3, 4, 5, 6 };
     
        int n = a.Length;
     
        Console.WriteLine(LIS(a, n));
    }
}
 
// This code is contributed by Ryuga


PHP


输出:
3

时间复杂度: O(N * log(N))

辅助空间: O(N)