// aome & ngkan
#include <bits/stdc++.h>
using namespace std;
const int N = 3005;
const int mod = 1e9 + 7;
int n, m, q, res = 1;
int prd[N], inv[N];
int f[N][N];
int mnX, mxX, mnY, mxY;
int lenX, lenY;
int pw(int x, int y) {
if (!y) return 1;
int ret = pw(x, y >> 1); ret = 1LL * ret * ret % mod;
if (y & 1) ret = 1LL * ret * x % mod; return ret;
}
int C(int n, int k) {
if (n < k) return 0;
return 1LL * prd[n] * inv[n - k] % mod * inv[k] % mod;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m >> q;
prd[0] = inv[0] = 1;
for (int i = 1; i < N; ++i) {
prd[i] = 1LL * prd[i - 1] * i % mod, inv[i] = pw(prd[i], mod - 2);
}
mnX = mnY = 1e9;
for (int i = 1; i <= q; ++i) {
int x, y; cin >> x >> y;
mnX = min(mnX, x), mnY = min(mnY, y);
mxX = max(mxX, x), mxY = max(mxY, y);
}
lenX = mxX - mnX + 1, lenY = mxY - mnY + 1;
int cntIn = lenX * lenY - q;
for (int i = 1; i <= cntIn; ++i) res = 1LL * res * i % mod;
f[lenX][lenY] = 1;
for (int i = lenX; i <= n; ++i) {
for (int j = lenY; j <= m; ++j) {
if (i == lenX && j == lenY) continue;
if (i != lenX) f[i][j] = (f[i][j] + 1LL * f[i - 1][j] * prd[j]) % mod;
if (j != lenY) f[i][j] = (f[i][j] + 1LL * f[i][j - 1] * prd[i]) % mod;
}
}
res = 1LL * res * f[n][m] % mod;
res = 1LL * res * C(n - lenX, n - mxX) % mod * C(m - lenY, m - mxY) % mod;
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
37472 KB |
Output is correct |
2 |
Correct |
0 ms |
37472 KB |
Output is correct |
3 |
Correct |
0 ms |
37472 KB |
Output is correct |
4 |
Correct |
0 ms |
37472 KB |
Output is correct |
5 |
Correct |
0 ms |
37472 KB |
Output is correct |
6 |
Correct |
0 ms |
37472 KB |
Output is correct |
7 |
Correct |
0 ms |
37472 KB |
Output is correct |
8 |
Correct |
0 ms |
37472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
37472 KB |
Output is correct |
2 |
Correct |
0 ms |
37472 KB |
Output is correct |
3 |
Correct |
0 ms |
37472 KB |
Output is correct |
4 |
Correct |
0 ms |
37472 KB |
Output is correct |
5 |
Correct |
0 ms |
37472 KB |
Output is correct |
6 |
Correct |
0 ms |
37472 KB |
Output is correct |
7 |
Correct |
0 ms |
37472 KB |
Output is correct |
8 |
Correct |
0 ms |
37472 KB |
Output is correct |
9 |
Correct |
0 ms |
37472 KB |
Output is correct |
10 |
Correct |
0 ms |
37472 KB |
Output is correct |
11 |
Correct |
0 ms |
37472 KB |
Output is correct |
12 |
Correct |
0 ms |
37472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
37472 KB |
Output is correct |
2 |
Correct |
3 ms |
37472 KB |
Output is correct |
3 |
Correct |
0 ms |
37472 KB |
Output is correct |
4 |
Correct |
9 ms |
37472 KB |
Output is correct |
5 |
Correct |
3 ms |
37472 KB |
Output is correct |
6 |
Correct |
19 ms |
37472 KB |
Output is correct |
7 |
Correct |
6 ms |
37472 KB |
Output is correct |
8 |
Correct |
0 ms |
37472 KB |
Output is correct |
9 |
Correct |
53 ms |
37472 KB |
Output is correct |
10 |
Correct |
73 ms |
37472 KB |
Output is correct |
11 |
Correct |
56 ms |
37472 KB |
Output is correct |
12 |
Correct |
73 ms |
37472 KB |
Output is correct |
13 |
Correct |
3 ms |
37472 KB |
Output is correct |
14 |
Correct |
63 ms |
37472 KB |
Output is correct |
15 |
Correct |
83 ms |
37472 KB |
Output is correct |
16 |
Correct |
66 ms |
37472 KB |
Output is correct |
17 |
Correct |
56 ms |
37472 KB |
Output is correct |
18 |
Correct |
89 ms |
37472 KB |
Output is correct |