Count arrays with GCD as D

678 Views Asked by At

Given N ,I need to count the number of array of integers which satisfy the following conditions :

1. Array must contain N elements.
2. Each element in array must be less than given number M.
3. Greatest common divisor of all its elements is equal to a given integer D.

I need to count such arrays for all D between L to R(including L and R).It means $D=L,L+1,L+2....R$.

Example : Let $ N=5 , M=5 ,L=4 $ and $R=5 $ then here answer is $2$.

How to find it for given $N,M,L$ and $R$.Please help

Can their be direct formula for this problem ? Or is their some proper way or algorithm to do it .