-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmanhattan-distances-of-all-arrangements-of-pieces.cpp
65 lines (57 loc) · 1.91 KB
/
manhattan-distances-of-all-arrangements-of-pieces.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Time: precompute: O(max(m * n))
// runtime: O(1)
// Space: O(max(m * n))
// combinatorics
static const uint32_t MOD = 1e9 + 7;
uint32_t addmod(uint32_t a, uint32_t b) { // avoid overflow
a %= MOD, b %= MOD;
if (MOD - a <= b) {
b -= MOD; // relied on unsigned integer overflow in order to give the expected results
}
return a + b;
}
// reference: https://stackoverflow.com/questions/12168348/ways-to-do-modulo-multiplication-with-primitive-types
uint32_t mulmod(uint32_t a, uint32_t b) { // avoid overflow
a %= MOD, b %= MOD;
uint32_t result = 0;
if (a < b) {
swap(a, b);
}
while (b > 0) {
if (b & 1) {
result = addmod(result, a);
}
a = addmod(a, a);
b >>= 1;
}
return result;
}
vector<int> FACT = {1, 1};
vector<int> INV = {1, 1};
vector<int> INV_FACT = {1, 1};
int nCr(int n, int k) {
while (size(INV) <= n) { // lazy initialization
FACT.emplace_back(mulmod(FACT.back(), size(INV)));
INV.emplace_back(mulmod(INV[MOD % size(INV)], MOD - MOD / size(INV))); // https://cp-algorithms.com/algebra/module-inverse.html
INV_FACT.emplace_back(mulmod(INV_FACT.back(), INV.back()));
}
return mulmod(mulmod(FACT[n], INV_FACT[n - k]), INV_FACT[k]);
}
class Solution {
public:
int distanceSum(int m, int n, int k) {
const auto& sum_n = [](int n) {
return static_cast<int64_t>(n + 1) * n / 2;
};
const auto& sum_n_square = [](int n) {
return static_cast<int64_t>(n) * (n + 1) * (2 * n + 1) / 6;
};
const auto& f = [&](int n) {
// sum((d*(n-d) for d in xrange(1, n)))
return (n * sum_n(n - 1) - sum_n_square(n - 1));
};
const auto x = mulmod(mulmod(f(n) % MOD, m), m);
const auto y = mulmod(mulmod(f(m) % MOD, n), n);
return mulmod(x + y, nCr(m * n - 2, k - 2));
}
};