📌  相关文章
📜  所有相邻元素都被一个相邻元素相除的数组的计数

📅  最后修改于: 2021-05-08 15:53:35             🧑  作者: Mango

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

  1. 每个元素都在[1,m]范围内
  2. 所有相邻元件是这样的,它们中的一个划分另一个即元件A I把一个I + 1或A i + 1的把一个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。现在观察每个索引,我们可以达到其倍数或倍数。因此,对其进行预计算并将其存储在所有元素中。因此,对于以整数x结尾的每个位置i,我们都可以转到下一个位置i + 1,该数组以以x的倍数或x的倍数结尾的整数结尾。同样,x的倍数或x的因数必须小于m。
因此,我们定义了一个2D数组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);
    }
  
    // Initalising 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);
    }
  
    // Initalising 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) 
  
    # Initalising 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);
    }
  
    // Initalising 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


输出:

17

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