diff --git a/solution/2400-2499/2435.Paths in Matrix Whose Sum Is Divisible by K/README.md b/solution/2400-2499/2435.Paths in Matrix Whose Sum Is Divisible by K/README.md index 7cac048a5fed5..9c71e0864b40b 100644 --- a/solution/2400-2499/2435.Paths in Matrix Whose Sum Is Divisible by K/README.md +++ b/solution/2400-2499/2435.Paths in Matrix Whose Sum Is Divisible by K/README.md @@ -255,6 +255,33 @@ impl Solution { } ``` +#### C# + +```cs +public class Solution { + public int NumberOfPaths(int[][] grid, int k) { + const int mod = (int) 1e9 + 7; + int m = grid.Length, n = grid[0].Length; + int[,,] f = new int[m, n, k]; + f[0, 0, grid[0][0] % k] = 1; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + for (int p = 0; p < k; ++p) { + int k0 = ((p - grid[i][j] % k) + k) % k; + if (i > 0) { + f[i, j, p] = (f[i, j, p] + f[i - 1, j, k0]) % mod; + } + if (j > 0) { + f[i, j, p] = (f[i, j, p] + f[i, j - 1, k0]) % mod; + } + } + } + } + return f[m - 1, n - 1, 0]; + } +} +``` + diff --git a/solution/2400-2499/2435.Paths in Matrix Whose Sum Is Divisible by K/README_EN.md b/solution/2400-2499/2435.Paths in Matrix Whose Sum Is Divisible by K/README_EN.md index e85d0a014a6d2..f8a571969dbba 100644 --- a/solution/2400-2499/2435.Paths in Matrix Whose Sum Is Divisible by K/README_EN.md +++ b/solution/2400-2499/2435.Paths in Matrix Whose Sum Is Divisible by K/README_EN.md @@ -254,6 +254,33 @@ impl Solution { } ``` +#### C# + +```cs +public class Solution { + public int NumberOfPaths(int[][] grid, int k) { + const int mod = (int) 1e9 + 7; + int m = grid.Length, n = grid[0].Length; + int[,,] f = new int[m, n, k]; + f[0, 0, grid[0][0] % k] = 1; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + for (int p = 0; p < k; ++p) { + int k0 = ((p - grid[i][j] % k) + k) % k; + if (i > 0) { + f[i, j, p] = (f[i, j, p] + f[i - 1, j, k0]) % mod; + } + if (j > 0) { + f[i, j, p] = (f[i, j, p] + f[i, j - 1, k0]) % mod; + } + } + } + } + return f[m - 1, n - 1, 0]; + } +} +``` + diff --git a/solution/2400-2499/2435.Paths in Matrix Whose Sum Is Divisible by K/Solution.cs b/solution/2400-2499/2435.Paths in Matrix Whose Sum Is Divisible by K/Solution.cs new file mode 100644 index 0000000000000..9ba61cb807455 --- /dev/null +++ b/solution/2400-2499/2435.Paths in Matrix Whose Sum Is Divisible by K/Solution.cs @@ -0,0 +1,22 @@ +public class Solution { + public int NumberOfPaths(int[][] grid, int k) { + const int mod = (int) 1e9 + 7; + int m = grid.Length, n = grid[0].Length; + int[,,] f = new int[m, n, k]; + f[0, 0, grid[0][0] % k] = 1; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + for (int p = 0; p < k; ++p) { + int k0 = ((p - grid[i][j] % k) + k) % k; + if (i > 0) { + f[i, j, p] = (f[i, j, p] + f[i - 1, j, k0]) % mod; + } + if (j > 0) { + f[i, j, p] = (f[i, j, p] + f[i, j - 1, k0]) % mod; + } + } + } + } + return f[m - 1, n - 1, 0]; + } +}