📜  计数长度为K的序列,其中每个术语都可以被其前一个术语整除

📅  最后修改于: 2021-06-26 13:49:21             🧑  作者: Mango

给定两个整数NK,该任务是找到长度K的序列的从区间[1,N],使得每个第(i + 1)序列中的元件是由它的前面的I整除组成值的数量元素

例子:

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

  1. 初始化矩阵fct [] []并将i的因数存储在fct [i]行中,其中i处于[1,N]范围内。
  2. 初始化矩阵dp [] [] ,该矩阵存储在dp [i] [j]处,长度为i的序列数以j结尾。
  3. 如果i索引具有j ,则第(i – 1)索引应由factor(j)组成。同样,第(i – 2)索引应由一个factor(factor(j))组成
  4. 因此, dp [i] [j]应该包含(i – 1)个长度为j的所有可能序列。
  5. 因此, dp [i] [j]等于所有可能的dp [i – 1] [fct [j] [k]]的和,其中dp [i – 1] [fct [j] [k]]表示以i的k因子结尾的长度为i – 1的总序列数。
  6. 最后,从1 <= j <= N找到所有dp [K] [j]的总和,并返回总和。

下面是上述方法的实现:

C++14
// C++ Program to implement the
// above approach
 
#include 
using namespace std;
 
#define ll long long
 
// Stores the factors of i-th
// element in v[i]
vector vp[2009];
 
// Function to find all the
// factors of N
void finding_factors(ll int n)
{
    ll int i, a;
 
    // Iterate upto sqrt(N)
    for (i = 1; i * i <= n; i++) {
 
        if (n % i == 0) {
 
            if (i * i == n) {
                vp[n].push_back(i);
            }
 
            else {
                vp[n].push_back(i);
                vp[n].push_back(n / i);
            }
        }
    }
}
 
// Function to return the count of
// sequences of length K having
// all terms divisible by its
// preceding term
ll int countSeq(ll int N, ll int K)
{
    ll int i, j, k;
 
    ll int dp[109][109] = { 0 };
 
    for (i = 1; i <= N; i++) {
 
        // Calculate factors of i
        finding_factors(i);
 
        // Initialize dp[0][i] = 0: No
        // subsequence of length 0 ending
        // with i-th element exists
        dp[0][i] = 0;
 
        // Initialize dp[0][i] = 1: Only 1
        // subsequence of length 1 ending
        // with i-th element exists
        dp[1][i] = 1;
    }
 
    // Iterate [2, K] to obtain sequences
    // of each length
    for (i = 2; i <= K; i++) {
 
        for (j = 1; j <= N; j++) {
 
            // Calculate sum of
            // all dp[i-1][vp[j][k]]
 
            ll int sum = 0;
 
            for (k = 0; k < vp[j].size(); k++) {
 
                // vp[j][k] stores all factors
                // of j
                sum = (sum + dp[i - 1][vp[j][k]]);
            }
 
            // Store the sum in A[i][j]
            dp[i][j] = sum;
        }
    }
 
    ll int ans = 0;
    for (j = 1; j <= N; j++) {
 
        // Sum of all dp[K][j] obtain all
        // K length sequences ending with j
        ans = (ans + dp[K][j]);
    }
    return ans;
}
 
// Driver code
int main()
{
 
    ll int N, K;
    N = 3;
    K = 2;
 
    cout << countSeq(N, K) << endl;
    return 0;
}


Java
// Java program to implement the
// above approach
import java.util.*;
 
class GFG{
 
// Stores the factors of i-th
// element in v[i]
@SuppressWarnings("unchecked")
static Vector []vp = new Vector[2009];
 
// Function to find all the
// factors of N
static void finding_factors(int n)
{
    int i, a;
 
    // Iterate upto Math.sqrt(N)
    for(i = 1; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            if (i * i == n)
            {
                vp[n].add(i);
            }
            else
            {
                vp[n].add(i);
                vp[n].add(n / i);
            }
        }
    }
}
 
// Function to return the count of
// sequences of length K having
// aint terms divisible by its
// preceding term
static int countSeq(int N, int K)
{
    int i, j, k;
 
    int dp[][] = new int[109][109];
 
    for(i = 1; i <= N; i++)
    {
 
        // Calculate factors of i
        finding_factors(i);
 
        // Initialize dp[0][i] = 0: No
        // subsequence of length 0 ending
        // with i-th element exists
        dp[0][i] = 0;
 
        // Initialize dp[0][i] = 1: Only 1
        // subsequence of length 1 ending
        // with i-th element exists
        dp[1][i] = 1;
    }
 
    // Iterate [2, K] to obtain sequences
    // of each length
    for(i = 2; i <= K; i++)
    {
        for(j = 1; j <= N; j++)
        {
 
            // Calculate sum of
            // aint dp[i-1][vp[j][k]]
            int sum = 0;
 
            for(k = 0; k < vp[j].size(); k++)
            {
 
                // vp[j][k] stores aint factors
                // of j
                sum = (sum + dp[i - 1][vp[j].get(k)]);
            }
 
            // Store the sum in A[i][j]
            dp[i][j] = sum;
        }
    }
 
    int ans = 0;
    for(j = 1; j <= N; j++)
    {
 
        // Sum of aint dp[K][j] obtain all
        // K length sequences ending with j
        ans = (ans + dp[K][j]);
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int N, K;
    N = 3;
    K = 2;
     
    for(int i = 0; i < vp.length; i++)
        vp[i] = new Vector();
         
    System.out.print(countSeq(N, K) + "\n");
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program to implement the
# above approach
 
# Stores the factors of i-th
# element in v[i]
vp = [[] for i in range(2009)]
 
# Function to find all the
# factors of N
def finding_factors(n):
     
    i = 1
    a = 0
     
    global vp
 
    # Iterate upto sqrt(N)
    while (i * i <= n):
        if (n % i == 0):
            if (i * i == n):
                vp[n].append(i)
            else:
                vp[n].append(i)
                vp[n].append(int(n / i))
                 
        i += 1
 
# Function to return the count of
# sequences of length K having
# all terms divisible by its
# preceding term
def countSeq(N, K):
     
    i = 0
    j = 0
    k = 0
 
    dp = [[0 for i in range(109)]
             for j in range(109)]
 
    for i in range(1, N + 1):
         
        # Calculate factors of i
        finding_factors(i)
 
        # Initialize dp[0][i] = 0: No
        # subsequence of length 0 ending
        # with i-th element exists
        dp[0][i] = 0
         
        # Initialize dp[0][i] = 1: Only 1
        # subsequence of length 1 ending
        # with i-th element exists
        dp[1][i] = 1
 
    # Iterate [2, K] to obtain sequences
    # of each length
    for i in range(2, K + 1):
        for j in range(1, N + 1):
             
            # Calculate sum of
            # all dp[i-1][vp[j][k]]
            Sum = 0
 
            for k in range(len(vp[j])):
                 
                # vp[j][k] stores all factors
                # of j
                Sum += dp[i - 1][vp[j][k]]
                 
            # Store the sum in A[i][j]
            dp[i][j] = Sum
 
    ans = 0
    for j in range(1, N + 1):
         
        # Sum of all dp[K][j] obtain all
        # K length sequences ending with j
        ans += dp[K][j]
 
    return ans
 
# Driver code
N = 3
K = 2
 
print(countSeq(N, K))
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program to implement the
// above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Stores the factors of i-th
// element in v[i]
static List []vp = new List[2009];
 
// Function to find all the
// factors of N
static void finding_factors(int n)
{
    int i ;
 
    // Iterate upto Math.Sqrt(N)
    for(i = 1; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            if (i * i == n)
            {
                vp[n].Add(i);
            }
            else
            {
                vp[n].Add(i);
                vp[n].Add(n / i);
            }
        }
    }
}
 
// Function to return the count of
// sequences of length K having
// aint terms divisible by its
// preceding term
static int countSeq(int N, int K)
{
    int i, j, k;
 
    int [,]dp = new int[109, 109];
 
    for(i = 1; i <= N; i++)
    {
 
        // Calculate factors of i
        finding_factors(i);
 
        // Initialize dp[0,i] = 0: No
        // subsequence of length 0 ending
        // with i-th element exists
        dp[0, i] = 0;
 
        // Initialize dp[0,i] = 1: Only 1
        // subsequence of length 1 ending
        // with i-th element exists
        dp[1, i] = 1;
    }
 
    // Iterate [2, K] to obtain sequences
    // of each length
    for(i = 2; i <= K; i++)
    {
        for(j = 1; j <= N; j++)
        {
 
            // Calculate sum of
            // aint dp[i-1,vp[j,k]]
            int sum = 0;
 
            for(k = 0; k < vp[j].Count; k++)
            {
 
                // vp[j,k] stores aint factors
                // of j
                sum = (sum + dp[i - 1, vp[j][k]]);
            }
 
            // Store the sum in A[i,j]
            dp[i,j] = sum;
        }
    }
 
    int ans = 0;
    for(j = 1; j <= N; j++)
    {
 
        // Sum of aint dp[K,j] obtain all
        // K length sequences ending with j
        ans = (ans + dp[K, j]);
    }
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    int N, K;
    N = 3;
    K = 2;
     
    for(int i = 0; i < vp.Length; i++)
        vp[i] = new List();
         
    Console.Write(countSeq(N, K) + "\n");
}
}
 
// This code is contributed by Rohit_ranjan


输出:
5

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。