Problem LinkAuthor: Ivan Fekete Tester: Misha Chorniy Editorialist: Bhuvnesh Jain DifficultyEASYMEDIUM PrerequisitesGCD, Inclusionexclusion, Counting ProblemGiven a matrix of size $(N, M)$. Select $K$ rows in increasing order and then a cell from each selected row. Find the GCD of the selected numbers. We need to find the sum of GCD of all possible ways of selecting rows and cells. ExplanationWe don't have any special property to GCD which helps us to extend the value when both adding or removing a particular number. Instead, we know that GCD of 2 numbers which divide both the numbers and also the divisors of GCD will also divide both the numbers. So, using this intuition, let us define $2$ arrays as follows: $A[d]$ denotes the numbers of ways to get the GCD of selected numbers as divisible by $d$. $B[d]$ denotes the numbers of ways to get the GCD of selected numbers as exactly $d$. Suppose, we are able to efficiently calculate array $A$, then we can simply find array $B$ and the final answer using the below pseudocode:
The idea for the above pseudocode is as follows:
The time complexity for this part is $O(X \log{X})$, where $X$ is the maximum element in the array i.e. ${10}^{5}$. So, our task is now reduced to finding array $A$ efficiently. Since we want the final GCD to be divisible by $d$, it means that all the numbers under consideration should be divisible by $d$. So, the task is to find the numbers of ways of selecting some rows and then cells from them such that each cell is divisible by $d$. First, let us calculate the number of ways to select a number divisible by $d$ from a row. Doing this naively means to iterate over all divisors of a given number and adding it's contribution to the particular row. This will take $O(N * M * \sigma{(A[i][j])})$, where $\sigma{(A[i][j])}$ denotes the number of divisors of $A[i][j]$ which can be atmost 128 as all numbers as less than ${10}^5$. But this is slow. Instead, we can use the below pseudocode to efficiently calculate this:
The idea is simply to calculate the frequency of each number in a row and then simultaneouly update all the divisors than to individually update the row when scanning a cell. Using the above pseudocode the complexity becomes $(N * M + N * (X/1 + X/2 + \cdots + 1))$ = $O(N * M + N * X * \log{X})$, where $X$ is the maximum element in the array i.e. ${10}^{5}$. Now, we have the frequency of numbers divisible by $d$ in each row. We need to find the numbers of ways to select some rows first and some cells the rows. Let us assume we have 4 rows with the number of cells divisible by $d$ as $w, x, y, z$ respectively. The number of ways is:
This can be written as $w * (x * (y + z + yz) + y + z + yz + x) + x * (y * z + y + z) + y * z$. See how the terms can be derived from the successive terms as most of it is same. For example, The first term in the bracket for $w$ is the same as the successive terms and the other term is similar to the term in the bracket of $x$. This idea is used by the editorialist in his solution. Other idea is to consider the product: $(1 + w) * (1 + x) * (1 + y) * (1 + z)$ and see how it relates to above terms we requires. The idea for choosing such term is as follows: Either we chose the term $w,x, y, z$ ways or we ignore it $1$ way. We can clearly see that the product only contains the extra terms of selecting exactly 1 row or none of the rows. So, in the above product, if we remove this contribution, we get our desired result. The author and tester have used this idea in their solution. Thus, we are able to calculate array $A$ efficiently as well. The time complexity of the above approach for calculating array $A$ is $O(N * M + N * X * \log{X} + N * X)$ where the first $2$ terms are for calculating the frequencies and the last term is to calculate the array from the frequency table. The overall time complexity of the solution is $O(N * M + N * X * \log{X} + N * X + X * \log{X})$. The space complexity is $O(N * M + N * X + X)$, where first term is for the matrix, the second term for the frequency table and the last terms for the arrays $A$ and $B$. Once, you are clear with the above idea, you can see the author or editorialist implementation below for help. Feel free to share your approach, if it was somewhat different. Time Complexity$O(N * M + N * X * \log{X} + N * X + X * \log{X})$ Space Complexity$O(N * M + N * X + X)$ AUTHOR'S AND TESTER'S SOLUTIONS:Author's solution can be found here. Tester's solution can be found here. Editorialist's solution can be found here.
This question is marked "community wiki".
asked 28 Jul '18, 16:55

In the second code snippet  is the third line correct? Shouldn't it be 
instead of
Let me know if I'm wrong. :) answered 30 Jul '18, 22:10

Concise and clear. Really liked the explanation. Kudos! answered 14 Aug '18, 18:43
