|
| 1 | +<h2><a href="https://leetcode.com/problems/paths-in-matrix-whose-sum-is-divisible-by-k">2521. Paths in Matrix Whose Sum Is Divisible by K</a></h2><h3>Hard</h3><hr><p>You are given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>grid</code> and an integer <code>k</code>. You are currently at position <code>(0, 0)</code> and you want to reach position <code>(m - 1, n - 1)</code> moving only <strong>down</strong> or <strong>right</strong>.</p> |
| 2 | + |
| 3 | +<p>Return<em> the number of paths where the sum of the elements on the path is divisible by </em><code>k</code>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> |
| 4 | + |
| 5 | +<p> </p> |
| 6 | +<p><strong class="example">Example 1:</strong></p> |
| 7 | +<img src="https://assets.leetcode.com/uploads/2022/08/13/image-20220813183124-1.png" style="width: 437px; height: 200px;" /> |
| 8 | +<pre> |
| 9 | +<strong>Input:</strong> grid = [[5,2,4],[3,0,5],[0,7,2]], k = 3 |
| 10 | +<strong>Output:</strong> 2 |
| 11 | +<strong>Explanation:</strong> There are two paths where the sum of the elements on the path is divisible by k. |
| 12 | +The first path highlighted in red has a sum of 5 + 2 + 4 + 5 + 2 = 18 which is divisible by 3. |
| 13 | +The second path highlighted in blue has a sum of 5 + 3 + 0 + 5 + 2 = 15 which is divisible by 3. |
| 14 | +</pre> |
| 15 | + |
| 16 | +<p><strong class="example">Example 2:</strong></p> |
| 17 | +<img src="https://assets.leetcode.com/uploads/2022/08/17/image-20220817112930-3.png" style="height: 85px; width: 132px;" /> |
| 18 | +<pre> |
| 19 | +<strong>Input:</strong> grid = [[0,0]], k = 5 |
| 20 | +<strong>Output:</strong> 1 |
| 21 | +<strong>Explanation:</strong> The path highlighted in red has a sum of 0 + 0 = 0 which is divisible by 5. |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p><strong class="example">Example 3:</strong></p> |
| 25 | +<img src="https://assets.leetcode.com/uploads/2022/08/12/image-20220812224605-3.png" style="width: 257px; height: 200px;" /> |
| 26 | +<pre> |
| 27 | +<strong>Input:</strong> grid = [[7,3,4,9],[2,3,6,2],[2,3,7,0]], k = 1 |
| 28 | +<strong>Output:</strong> 10 |
| 29 | +<strong>Explanation:</strong> Every integer is divisible by 1 so the sum of the elements on every possible path is divisible by k. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | +<p><strong>Constraints:</strong></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li><code>m == grid.length</code></li> |
| 37 | + <li><code>n == grid[i].length</code></li> |
| 38 | + <li><code>1 <= m, n <= 5 * 10<sup>4</sup></code></li> |
| 39 | + <li><code>1 <= m * n <= 5 * 10<sup>4</sup></code></li> |
| 40 | + <li><code>0 <= grid[i][j] <= 100</code></li> |
| 41 | + <li><code>1 <= k <= 50</code></li> |
| 42 | +</ul> |
0 commit comments