Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given three integers m, n, and k.
There is a rectangular grid of size m × n containing k identical pieces. Return the sum of Manhattan distances between every pair of pieces over all valid arrangements of pieces.
A valid arrangement is a placement of all k pieces on the grid with at most one piece per cell.
Since the answer may be very large, return it modulo 109 + 7.
The Manhattan Distance between two cells (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.
Example 1:
Input: m = 2, n = 2, k = 2
Output: 8
Explanation:
The valid arrangements of pieces on the board are:
Thus, the total Manhattan distance across all valid arrangements is 1 + 1 + 1 + 1 + 2 + 2 = 8.
Example 2:
Input: m = 1, n = 4, k = 3
Output: 20
Explanation:
The valid arrangements of pieces on the board are:
1 + 1 + 2 = 4.1 + 2 + 3 = 6.The total Manhattan distance between all pairs of pieces across all arrangements is 4 + 6 + 6 + 4 = 20.
Constraints:
1 <= m, n <= 1052 <= m * n <= 1052 <= k <= m * nProblem summary: You are given three integers m, n, and k. There is a rectangular grid of size m × n containing k identical pieces. Return the sum of Manhattan distances between every pair of pieces over all valid arrangements of pieces. A valid arrangement is a placement of all k pieces on the grid with at most one piece per cell. Since the answer may be very large, return it modulo 109 + 7. The Manhattan Distance between two cells (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
2 2 2
1 4 3
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3426: Manhattan Distances of All Arrangements of Pieces
class Solution {
public int distanceSum(int m, int n, int k) {
final long rowContrib = 1L * n * n * (1L * m * m * m - m) / 6 % MOD;
final long colContrib = 1L * m * m * (1L * n * n * n - n) / 6 % MOD;
return (int) ((rowContrib + colContrib) % MOD * nCk(m * n - 2, k - 2) % MOD);
}
private static final int MOD = 1_000_000_007;
private long nCk(int n, int k) {
long res = 1;
for (int i = 1; i <= k; ++i)
// By Fermat's little theorem.
res = res * (n - i + 1) % MOD * modPow(i, MOD - 2) % MOD;
return res;
}
private long modPow(long x, long n) {
if (n == 0)
return 1;
if (n % 2 == 1)
return x * modPow(x, n - 1) % MOD;
return modPow(x * x % MOD, n / 2);
}
}
// Accepted solution for LeetCode #3426: Manhattan Distances of All Arrangements of Pieces
package main
// https://space.bilibili.com/206214
const mod = 1_000_000_007
const mx = 100_000
var f [mx]int // f[i] = i!
var invF [mx]int // invF[i] = i!^-1
func init() {
f[0] = 1
for i := 1; i < mx; i++ {
f[i] = f[i-1] * i % mod
}
invF[mx-1] = pow(f[mx-1], mod-2)
for i := mx - 1; i > 0; i-- {
invF[i-1] = invF[i] * i % mod
}
}
func pow(x, n int) int {
res := 1
for ; n > 0; n /= 2 {
if n%2 > 0 {
res = res * x % mod
}
x = x * x % mod
}
return res
}
func comb(n, m int) int {
return f[n] * invF[m] % mod * invF[n-m] % mod
}
func distanceSum(m, n, k int) int {
return (m * n * (m*(n*n-1) + n*(m*m-1))) / 6 % mod * comb(m*n-2, k-2) % mod
}
# Accepted solution for LeetCode #3426: Manhattan Distances of All Arrangements of Pieces
class Solution:
def distanceSum(self, m: int, n: int, k: int) -> int:
# For each distance d, where 1 < d < m, there are `m - d` ways to choose
# the two columns that the two pieces are on. For each of the two pieces,
# there are `n` ways to choose the row that the piece is on.
# Therefore, the contribution of row differences is
# sum(d * (m - d) * n^2), where 1 < d <= m - 1
# = n^2 * sum(d * m - d^2)
# = n^2 * (d * m * (m - 1) / 2 - m * (m - 1) * (2m - 1) / 6)
# = n^2 * (m^3 - m) / 6
# Similarly, the contribution of column differences is
# m^2 * (n^3 - n) / 6
MOD = 1_000_000_007
return (n**2 * (m**3 - m) // 6 +
m**2 * (n**3 - n) // 6) * math.comb(m * n - 2, k - 2) % MOD
// Accepted solution for LeetCode #3426: Manhattan Distances of All Arrangements of Pieces
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3426: Manhattan Distances of All Arrangements of Pieces
// class Solution {
// public int distanceSum(int m, int n, int k) {
// final long rowContrib = 1L * n * n * (1L * m * m * m - m) / 6 % MOD;
// final long colContrib = 1L * m * m * (1L * n * n * n - n) / 6 % MOD;
// return (int) ((rowContrib + colContrib) % MOD * nCk(m * n - 2, k - 2) % MOD);
// }
//
// private static final int MOD = 1_000_000_007;
//
// private long nCk(int n, int k) {
// long res = 1;
// for (int i = 1; i <= k; ++i)
// // By Fermat's little theorem.
// res = res * (n - i + 1) % MOD * modPow(i, MOD - 2) % MOD;
// return res;
// }
//
// private long modPow(long x, long n) {
// if (n == 0)
// return 1;
// if (n % 2 == 1)
// return x * modPow(x, n - 1) % MOD;
// return modPow(x * x % MOD, n / 2);
// }
// }
// Accepted solution for LeetCode #3426: Manhattan Distances of All Arrangements of Pieces
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3426: Manhattan Distances of All Arrangements of Pieces
// class Solution {
// public int distanceSum(int m, int n, int k) {
// final long rowContrib = 1L * n * n * (1L * m * m * m - m) / 6 % MOD;
// final long colContrib = 1L * m * m * (1L * n * n * n - n) / 6 % MOD;
// return (int) ((rowContrib + colContrib) % MOD * nCk(m * n - 2, k - 2) % MOD);
// }
//
// private static final int MOD = 1_000_000_007;
//
// private long nCk(int n, int k) {
// long res = 1;
// for (int i = 1; i <= k; ++i)
// // By Fermat's little theorem.
// res = res * (n - i + 1) % MOD * modPow(i, MOD - 2) % MOD;
// return res;
// }
//
// private long modPow(long x, long n) {
// if (n == 0)
// return 1;
// if (n % 2 == 1)
// return x * modPow(x, n - 1) % MOD;
// return modPow(x * x % MOD, n / 2);
// }
// }
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.