Count in array
Medium Accuracy: 51.46% Submissions: 68 Points: 4

Given two positive integer N and M. The task is to find the number of arrays of size N that can be formed such that:

1. Each element is in the range [1, M].

2. All adjacent element are such that one of them divide the another i.e element Ai divides Ai + 1 or Ai+1 divides Ai.


Example 1:

Input: 
N = 3
M = 3
Output : 
17
Explanation:
{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.

Example 2:

Input: 
N = 1
M = 10 
Output: 
10
Explanation: 
{1}, {2}, {3}, {4}, {5}, 
{6}, {7}, {8}, {9}, {10}
are possible arrays.


Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which take integers N and M as input parameter and returns the total the number of arrays of size N that can be formed with given constraints. The return value may long so take modulo 109+7. 

Expected Time Complexity: O(N*M*Log M) 
Expected Space Complexity: O(N*M) 

Constraints:
1<=N,M<=100

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.


to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Count in array

Output Window