F - Minimum Bounding Box 2 Editorial by balakrishnan_v
Inclusion-exclusionLet \(f(h,w)\) be the number of ways to place K cells in a \(h\times w\) grid such that the minimal rectangle required to enclose all of them is the whole grid. This is simply the number of ways to place K cells in a \(h\times w\) grid minus the number of ways to place K cells on all sub-rectangles of size \(h'\times w'\) for \(h'<h\) or \(w'<w\) such that the corresponding smaller sub-rectangle is the minimal enclosing rectangle.
There are \((h-h'+1)(w-w'+1)\) such sub-rectangles for any size \(h'\times w'\). Using this we may formulate the recursion:
\( f(h,w)=\binom{hw}{K}-\sum_{(h'<h) \lor (w'<w)} (h-h'+1)(w-w'+1)f(h',w') \)
There are four terms in this summation:
- \(C_0(h,w)=\sum_{h',w'} f(h',w') \)
- \(C_1(h,w)=\sum_{h',w'} w'f(h',w') \)
- \(C_2(h,w)=\sum_{h',w'} h'f(h',w') \)
- \(C_3(h'w)=\sum_{h',w'} h'w'f(h',w') \) each of which can be suitably computed using the standard 2D cumulative sum approach. Finally \(f(h,w)\) is given as
\(f(h,w)=\binom{hw}{K}-(h+1)(w+1)C_0(h,w)+(h+1)C_1(h,w)+(w+1)C_2(h,w)-C_3(h,w)\).
Thus the total number of ways to enclose \(K\) cells within a grid of size \(h\times w\) in the original rectangle is
\((H-h+1)(W-w+1)f(h,w)\). Then the expected area becomes
\(E(H,W,K)=\frac{1}{\binom{HW}{K}} \sum_{h\leq H} \sum_{w \leq W} (H-h+1)(W-w+1)f(h,w) \times hw\)
#include<bits/stdc++.h>
#define MAXN 1000
#define MODD 998244353
#define ll long long
using namespace std;
ll fact[MAXN*MAXN+2];
ll factinv[MAXN*MAXN+2];
ll modpow(ll x, ll y) {
ll xs = x;
ll answer = 1;
while(y) {
if (y&1) {
answer = (answer * xs) % MODD;
}
y >>= 1;
xs = (xs * xs) % MODD ;
}
return answer;
}
ll B(ll x, ll y) {
if (x < y) return 0;
if (y == 0) return 1;
return fact[x]*factinv[y]%MODD*factinv[x-y]%MODD;
}
ll ways[MAXN+1][MAXN+1];
ll C[4][MAXN+1][MAXN+1];
int main() {
int H,W,K;
cin>>H>>W>>K;
fact[0]=factinv[0]=1;
for(int i=1;i<=H*W;i++) {
fact[i]=(fact[i-1]*i)%MODD;
factinv[i]=modpow(fact[i],MODD-2);
}
ll ans=0;
for(int h=1;h<=H;h++) {
for(int w=1;w<=W;w++) {
ll ways_to_enclose_smaller=0;
ways_to_enclose_smaller += (h+1)*(w+1)*(C[0][h-1][w]+C[0][h][w-1]-C[0][h-1][w-1]);
ways_to_enclose_smaller -= (h+1)*(C[1][h-1][w]+C[1][h][w-1]-C[1][h-1][w-1]);
ways_to_enclose_smaller -= (w+1)*(C[2][h-1][w]+C[2][h][w-1]-C[2][h-1][w-1]);
ways_to_enclose_smaller += (C[3][h-1][w]+C[3][h][w-1]-C[3][h-1][w-1]);
ways_to_enclose_smaller %= MODD;
ways[h][w]=B(h*w,K)-ways_to_enclose_smaller;
ways[h][w]%=MODD;
if (ways[h][w]<0)ways[h][w]+=MODD;
ans += h*w*(H-h+1ll)*(W-w+1ll)%MODD*ways[h][w]%MODD;
ans %= MODD;
C[0][h][w]=ways[h][w]+C[0][h-1][w]+C[0][h][w-1]-C[0][h-1][w-1];
C[1][h][w]=w*ways[h][w]+C[1][h-1][w]+C[1][h][w-1]-C[1][h-1][w-1];
C[2][h][w]=h*ways[h][w]+C[2][h-1][w]+C[2][h][w-1]-C[2][h-1][w-1];
C[3][h][w]=w*h*ways[h][w]+C[3][h-1][w]+C[3][h][w-1]-C[3][h-1][w-1];
for(int j=0;j<4;j++) {
C[j][h][w]%=MODD;
if (C[j][h][w]<0) C[j][h][w]+=MODD;
}
}
}
cout << ans*modpow(B(H*W,K),MODD-2)%MODD << endl;
}
posted:
last update: