News Leaflets
A leading news portal.

Count of N size Arrays with each element as multiple or divisor of its neighbours

0

Given two numbers N and K, the task is to count the number of all possible arrays of size N such that each element is a positive integer less than or equal to K and is either a multiple or a divisor of its neighbours. Since the answer can be large, print it modulo 109 + 7.

Examples: 

Input: N = 2, K = 3
Output: 7
Explanation: All the possible arrays are – { {1, 2}, {2, 1}, {1, 3}, {3, 1}, {1, 1}, {2, 2}, {3, 3} }

Input: N = 5, K = 4
Output: 380

 

Naive Approach: The simplest approach is to find all combinations of arrays of size N where each element is less than or equal to ‘K’, and for each combination check if adjacent elements are multiples of each other or not. 

Time Complexity: O(KN * N)
Auxiliary Space: O(N)

Efficient Approach: The above approach can also be optimized by using Dynamic Programming because of its overlapping subproblems and optimal substructure property using the following observation: 

The subproblems can be stored in dp[][] table using memoization where dp[i][prev] stores the count of all possible arrays from the ith position till the end, with prev as the value in (i-t)th index. 

Follow the steps below to solve the problem:

  • Initialize a global multidimensional array dp[][] to store the result of each recursive call.
  • Find the multiples and divisors of all numbers from 1 to K and store them.
  • Define a recursive function and perform the following operations:
    • If the value of i is N, return 1 as a valid array has been formed.
    • If the result of the state dp[i][prev] is already computed, return that calculated value.
    • Iterate through all the multiples and divisors of ‘prev‘, and for each number call the recursive function for (i + 1)th index.
  • The value at dp[0][1] will be the required answer.

Below is the implementation of the above approach : 

C++

#include <bits/stdc++.h>

using namespace std;

  

int dp[1005][1005];

  

vector<vector<int> > info(1005);

  

int countOfArrays(int index, int prev, int N)

{

    

    

    if (index == N) {

        return 1;

    }

  

    

    if (dp[index][prev] != -1) {

        return dp[index][prev];

    }

    dp[index][prev] = 0;

  

    

    

    for (auto num : info[prev]) {

  

        

        

        dp[index][prev]

            += countOfArrays(index + 1,

                             num, N);

  

        

        dp[index][prev] %= 1000000007;

    }

  

    

    return dp[index][prev];

}

  

int UtilCountOfArrays(int N, int K)

{

  

    

    memset(dp, -1, sizeof dp);

  

    

    

    for (int i = 1; i <= K; ++i) {

        for (int j = 1; j <= K; ++j) {

            if (i % j == 0 or j % i == 0) {

                info[i].push_back(j);

            }

        }

    }

  

    

    return countOfArrays(0, 1, N);

}

  

int main()

{

    

    int N = 2;

    int K = 3;

    cout << UtilCountOfArrays(N, K) << endl;

    return 0;

}

Time Complexity: O(N * K * √K)
Auxiliary Space: O(N * K)

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! News Leaflets is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment
vulvatube.com teenpornvideo.mobi desi girls sexy
sex video in bus tubekitty.mobi kamapichachi
the broken marriage vow march 12 full episode teleseryepisode.com mateo lorenzo net worth
telugu hd sex pornflex.org bluefilmtamil
indian sex stories lesbian indaporn.com best sex mms
hinde xnxx video redwap2.com bangla chudachudi
طيز خليجي filmstreamingporno.com التحرش بالمنقبات
affair sex video ultraporn.mobi deshimagi
tubb99 nuporn.mobi mumbaixvideo
agimat ng agila cast watchpinoyteleserye.com stl today result
age of tamanna pimpmovs.com xxxxx inden
indan xvideo com xxx-tube-list.net indina six video
gonzo xxx sunny leone eporner.name desi pirn
بنت تلعب في كسها teentubeonline.com سكس امهات اسيوي
largeporn film tubepatrol.porn kama katai