This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 3e3 + 5, MD = 1e9 + 7;
int add(int a, int b) {
if ((a += b) >= MD) {
a -= MD;
}
return a;
}
int fact[N * N], dp[N][N], C[N][N];
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int r, c, n; cin >> r >> c >> n;
fact[0] = 1;
for (int i = 1; i <= r * c; ++i) {
fact[i] = (long long) fact[i - 1] * i % MD;
}
for (int i = 0; i <= max(r, c); ++i) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; ++j) {
C[i][j] = add(C[i - 1][j], C[i - 1][j - 1]);
}
}
array<int, 2> mi{r, c}, ma{};
for (int _ = 0; _ < n; ++_) {
for (int i = 0; i < 2; ++i) {
int x; cin >> x;
mi[i] = min(mi[i], x);
ma[i] = max(ma[i], x);
}
}
int a = ma[0] - mi[0] + 1, b = ma[1] - mi[1] + 1;
dp[a][b] = fact[a * b - n];
for (int i = a; i <= r; ++i) {
for (int j = b; j <= c; ++j) {
dp[i][j] = add(dp[i][j], (long long) dp[i - 1][j] * fact[j] % MD);
dp[i][j] = add(dp[i][j], (long long) dp[i][j - 1] * fact[i] % MD);
}
}
auto res = (long long) dp[r][c] * C[r - a][mi[0] - 1] % MD * C[c - b][mi[1] - 1] % MD;
cout << res;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |