Recently I got this question in a hiring challenge. I am unable to think of any suitable approach. Please someone help me solve the problem.
"You have an infinite supply of K items (labelled from 1 to K) and an integer N. YOU have to distribute the K items among N people such that the label number of the item received by the ith person is divisible by the label number of the item received by the (i+1)th person or vice-versa.
For example, if the ith person receives an item with the label X and the (i+1)th person receives the item with the label Y, then either X is divisible by Y or Y is divisible by X. Write a program to find the total number of distinct ways of distributing the K items among N people modulo 10^9 + 7."
Constraints:
1<= T <= 100 (T : test cases)
1<= N, K <= 1000
Sample:
N = 2, K = 2, Ans = 4
N = 5, K = 3, Ans = 99








Pretty sure you're looking for this — https://www.spoj.com/problems/DIVSEQ/, the samples don't match the problem you described anyway.
But the problem you mentioned is interesting, couldn't think of anything good.
Edit: Please ignore, I misunderstood the problem. It's exactly DIVSEQ.
Can't we use DP ? I mean the values do not exceed 1000 so the state can be the index and the last value, next values are either multiples or divisors of the last value isn't it ?
Yeah, this works. Naive implementation is $$$\mathcal{O}(NK^2)$$$, but you immediately get $$$\mathcal{O}(NK \log K)$$$ by precalculating divisors and multiples below $$$k+1$$$ of every number (There's in total $$$\mathcal{O}(K \log K)$$$ of them)
Yes exactly!
Oh sorry my bad, I read the problem wrong. The problem in this blog is the same as the one in spoj, which is easily solvable using dp.
So this is what I understood initially — Distribute k items in n people such that ith and (i+1)th number of items are factor-multiple pair. Still don't know how to do this under the same constraints, k,n<=1000