Ctrl K

Grid Paths With a Divisible Sum

Hard
You are given a field field with m rows and n columns of non-negative integers, and an integer divisor. You start in the top-left cell (0, 0) and walk to the bottom-right cell (m − 1, n − 1), moving only down or right. The sum of a path is the sum of the numbers in every cell it visits, including the first and the last. Return how many paths have a sum divisible by divisor. The answer can be large, so return it modulo 10⁹ + 7.
Example 1
field=[[1, 2], [3, 4]], divisor=4 → 1

There are two paths: 1 → 2 → 4 with sum 7 and 1 → 3 → 4 with sum 8. Only the second is divisible by 4.

Example 2
field=[[2, 4, 6]], divisor=3 → 1

The only path runs right along the row, and its sum 12 is divisible by 3.

Example 3
field=[[0, 5, 1], [3, 2, 9]], divisor=7 → 1
Constraints
  • 1 ≤ m, n, m·n ≤ 5·10⁴
  • 0 ≤ field[i][j] ≤ 100
  • 1 ≤ divisor ≤ 50
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Solid Panel Block HardCrossing the Cave HardLongest Uphill Walk HardWeeks of Rest by Flight HardBerry Field Round Trip Hard
loading Python…
Input
field=[[1, 2], [3, 4]], divisor=4
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 73 tests on the server, hidden ones included.