📌  相关文章
📜  数组的计数,其中所有相邻元素都是这样的,其中一个将另一个分开

📅  最后修改于: 2021-09-22 10:31:31             🧑  作者: Mango

给定两个正整数nn 。任务是找到可以形成的大小为 n 的数组的数量,以便:

  1. 每个元素都在 [1, m] 范围内
  2. 所有相邻元素都是这样的,它们中的一个划分另一个,即元素 A i划分 A i + 1或 A i + 1划分 A i + 2

例子:

Input : n = 3, m = 3.
Output : 17
{1,1,1}, {1,1,2}, {1,1,3}, {1,2,1}, 
{1,2,2}, {1,3,1}, {1,3,3}, {2,1,1},
{2,1,2}, {2,1,3}, {2,2,1}, {2,2,2},
{3,1,1}, {3,1,2}, {3,1,3}, {3,3,1}, 
{3,3,3} are possible arrays.

Input : n = 1, m = 10.
Output : 10 

我们尝试在数组的每个索引处找到可能值的数量。首先,在索引 0 处,所有值都可能从 1 到 m。现在观察每个索引,我们可以达到它的倍数或它的因数。因此,预先计算并为所有元素存储它。因此,对于每个位置 i,以整数 x 结尾,我们可以转到下一个位置 i + 1,数组以整数结尾,x 的倍数或 x 的因子。此外,x 的倍数或 x 的因子必须小于 m。

因此,我们定义了一个二维数组 dp[i][j],它是大小为 i 的可能数组(可整除的相邻元素)的数量,其中 j 作为其第一个索引元素。

1 <= i <= m, dp[1][m] = 1.
for 1 <= j <= m and 2 <= i <= n
  dp[i][j] = dp[i-1][j] + number of factor 
             of previous element less than m 
             + number of multiples of previous
             element less than m.

下面是这个方法的实现:

C++
// C++ program to count number of arrays
// of size n such that every element is
// in range [1, m] and adjacen are
// divisible
#include 
#define  MAX 1000
using namespace std;
 
int numofArray(int n, int m)
{
    int dp[MAX][MAX];
 
    // For storing factors.
    vector di[MAX];
 
    // For storing multiples.
    vector mu[MAX];
 
    memset(dp, 0, sizeof dp);
    memset(di, 0, sizeof di);
    memset(mu, 0, sizeof mu);
 
    // calculating the factors and multiples
    // of elements [1...m].
    for (int i = 1; i <= m; i++)
    {
        for (int j = 2*i; j <= m; j += i)
        {
            di[j].push_back(i);
            mu[i].push_back(j);
        }
        di[i].push_back(i);
    }
 
    // Initialising for size 1 array for
    // each i <= m.
    for (int i = 1; i <= m; i++)
        dp[1][i] = 1;
 
    // Calculating the number of array possible
    // of size i and starting with j.
    for (int i = 2; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            dp[i][j] = 0;
 
            // For all previous possible values.
            // Adding number of factors.
            for (auto x:di[j])
                dp[i][j] += dp[i-1][x];
 
            // Adding number of multiple.
            for (auto x:mu[j])
                dp[i][j] += dp[i-1][x];
        }
    }
 
    // Calculating the total count of array
    // which start from [1...m].
    int ans = 0;
    for (int i = 1; i <= m; i++)
    {
        ans += dp[n][i];
        di[i].clear();
        mu[i].clear();
    }
 
    return ans;
}
 
// Driven Program
int main()
{
    int n = 3, m = 3;
    cout << numofArray(n, m) << "\n";
    return 0;
}


Java
// Java program to count number of arrays
// of size n such that every element is
// in range [1, m] and adjacen are
// divisible
import java.util.*;
 
class GFG
{
static int MAX = 1000;
 
static int numofArray(int n, int m)
{
    int [][]dp = new int[MAX][MAX];
 
    // For storing factors.
    Vector []di = new Vector[MAX];
 
    // For storing multiples.
    Vector []mu = new Vector[MAX];
 
    for(int i = 0; i < MAX; i++)
    {
        for(int j = 0; j < MAX; j++)
        {
            dp[i][j] = 0;
        }
    }
    for(int i = 0; i < MAX; i++)
    {
        di[i] = new Vector<>();
        mu[i] = new Vector<>();
    }
     
    // calculating the factors and multiples
    // of elements [1...m].
    for (int i = 1; i <= m; i++)
    {
        for (int j = 2 * i; j <= m; j += i)
        {
            di[j].add(i);
            mu[i].add(j);
        }
        di[i].add(i);
    }
 
    // Initialising for size 1 array for
    // each i <= m.
    for (int i = 1; i <= m; i++)
        dp[1][i] = 1;
 
    // Calculating the number of array possible
    // of size i and starting with j.
    for (int i = 2; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            dp[i][j] = 0;
 
            // For all previous possible values.
            // Adding number of factors.
            for (Integer x:di[j])
                dp[i][j] += dp[i - 1][x];
 
            // Adding number of multiple.
            for (Integer x:mu[j])
                dp[i][j] += dp[i - 1][x];
        }
    }
 
    // Calculating the total count of array
    // which start from [1...m].
    int ans = 0;
    for (int i = 1; i <= m; i++)
    {
        ans += dp[n][i];
        di[i].clear();
        mu[i].clear();
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 3, m = 3;
    System.out.println(numofArray(n, m));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to count the number of
# arrays of size n such that every element is
# in range [1, m] and adjacent are divisible
 
MAX = 1000
 
def numofArray(n, m):
  
    dp = [[0 for i in range(MAX)] for j in range(MAX)]
 
    # For storing factors.
    di = [[] for i in range(MAX)]
 
    # For storing multiples.
    mu = [[] for i in range(MAX)]
 
    # calculating the factors and multiples
    # of elements [1...m].
    for i in range(1, m+1):
      
        for j in range(2*i, m+1, i):
          
            di[j].append(i)
            mu[i].append(j)
          
        di[i].append(i)
 
    # Initialising for size 1 array for each i <= m.
    for i in range(1, m+1):
        dp[1][i] = 1
 
    # Calculating the number of array possible
    # of size i and starting with j.
    for i in range(2, n+1):
      
        for j in range(1, m+1):
          
            dp[i][j] = 0
 
            # For all previous possible values.
            # Adding number of factors.
            for x in di[j]:
                dp[i][j] += dp[i-1][x]
 
            # Adding number of multiple.
            for x in mu[j]:
                dp[i][j] += dp[i-1][x]
          
    # Calculating the total count of array
    # which start from [1...m].
    ans = 0
    for i in range(1, m+1):
      
        ans += dp[n][i]
        di[i].clear()
        mu[i].clear()
     
    return ans
  
# Driven Program
if __name__ == "__main__":
  
    n = m = 3
    print(numofArray(n, m))
     
# This code is contributed by Rituraj Jain


C#
// C# program to count number of arrays
// of size n such that every element is
// in range [1, m] and adjacen are
// divisible
using System;
using System.Collections.Generic;                
     
class GFG
{
static int MAX = 1000;
 
static int numofArray(int n, int m)
{
    int [,]dp = new int[MAX, MAX];
 
    // For storing factors.
    List []di = new List[MAX];
 
    // For storing multiples.
    List []mu = new List[MAX];
 
    for(int i = 0; i < MAX; i++)
    {
        for(int j = 0; j < MAX; j++)
        {
            dp[i, j] = 0;
        }
    }
    for(int i = 0; i < MAX; i++)
    {
        di[i] = new List();
        mu[i] = new List();
    }
     
    // calculating the factors and multiples
    // of elements [1...m].
    for (int i = 1; i <= m; i++)
    {
        for (int j = 2 * i; j <= m; j += i)
        {
            di[j].Add(i);
            mu[i].Add(j);
        }
        di[i].Add(i);
    }
 
    // Initialising for size 1 array for
    // each i <= m.
    for (int i = 1; i <= m; i++)
        dp[1, i] = 1;
 
    // Calculating the number of array possible
    // of size i and starting with j.
    for (int i = 2; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            dp[i, j] = 0;
 
            // For all previous possible values.
            // Adding number of factors.
            foreach (int x in di[j])
                dp[i, j] += dp[i - 1, x];
 
            // Adding number of multiple.
            foreach (int x in mu[j])
                dp[i, j] += dp[i - 1, x];
        }
    }
 
    // Calculating the total count of array
    // which start from [1...m].
    int ans = 0;
    for (int i = 1; i <= m; i++)
    {
        ans += dp[n, i];
        di[i].Clear();
        mu[i].Clear();
    }
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 3, m = 3;
    Console.WriteLine(numofArray(n, m));
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:

17

时间复杂度: O(N*M)。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程