#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"
using ll = long long;
using ld = long double;
template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = numeric_limits<ll>::max() / 8;
int add(int x, int y){
return (x + y < MOD ? x + y : x + y - MOD);
}
int mul(int x, int y){
return (1ll * x * y) % MOD;
}
int bpow(int a, int b){
int ans = 1; assert(b >= 0);
for(; b; b >>= 1, a = mul(a, a)){
if(b & 1) ans = mul(ans, a);
}
return ans;
}
void Main()
{
int n,m; cin >> n >> m;
int k; cin >> k;
int x1 = n, x2 = 0, y1 = m, y2 = 0;
FOR(i, 1, k){
int a,b; cin >> a >> b;
ckmn(x1, a), ckmx(x2, a);
ckmn(y1, b), ckmx(y2, b);
}
vi fact(n * m + 1, 0), invf(n * m + 1, 0);
fact[0] = 1, invf[0] = 1;
FOR(i, 1, n * m){
fact[i] = mul(fact[i - 1], i);
invf[i] = bpow(fact[i], MOD - 2);
}
auto C = [&](int n, int k) -> int{
return mul(fact[n], mul(invf[n - k], invf[k]));
};
int got_x = x2 - x1 + 1, got_y = y2 - y1 + 1;
int needx = n - got_x, needy = m - got_y;
// How to build a full rectangle that get max happiness
vector<vi> dp(needx + 1, vi(needy + 1, 0));
dp[0][0] = 1;
FOR(i, 0, needx){
FOR(j, 0, needy){
if(!i && !j) continue;
if(i) dp[i][j] = add(dp[i][j], mul(dp[i - 1][j], fact[j + got_y]));
if(j) dp[i][j] = add(dp[i][j], mul(dp[i][j - 1], fact[i + got_x]));
}
}
int ans = dp[needx][needy];
// we need to care the order of choosing row, column
ans = mul(ans, mul(C(needx, x1 - 1), C(needy, y1 - 1)));
// how to build a rectangle [x1, y1, x2, y2] ?
ans = mul(ans, fact[got_x * got_y - k]);
cout << ans << el;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
int t = 1; while(t--) Main();
return 0;
}
Compilation message (stderr)
mascots.cpp: In function 'int32_t main()':
mascots.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mascots.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | freopen(name".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |