AtCoder Regular Contest 162 F Montage
题目限制可以被改写成,如果 \(A_{a, b} = A_{c, d} = 1\),那么 \(A_{a, d} = A_{c, b} = 1\)。
考虑删去空白的行和列,那么对于每个 \(A_{a, b} = A_{c, d} = 1\),矩形 \((a, b), (c, d)\) 中一定都是 \(1\)。
发现每一行只可能存在一个极长 \(1\) 区间。并且对于第 \(i\) 行的这个区间 \([l_i, r_i]\),有 \(l_i \le l_{i - 1} \le r_i + 1, r_i \le r_{i - 1}\)。也就是说最终的 \(A\) 长这样:
这是一个很强的性质。已经可以 dp 了,设 \(f_{i, j, k}\) 为考虑到第 \(i\) 行,\(l_i = j, r_i = k\) 的方案数。转移容易二维前缀和优化。
统计答案时,注意我们是在删掉空行和空列的基础上讨论的,因此 \(f_{i, j, k}\) 对答案的贡献系数是在 \(n \times m\) 的矩阵中选 \(i\) 行,\(m - j + 1\) 列的方案数即 \(\binom{n}{i} \times \binom {m}{m - j + 1}\)。
code
// Problem: F - Montage
// Contest: AtCoder - AtCoder Regular Contest 162
// URL: https://atcoder.jp/contests/arc162/tasks/arc162_f
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;
const int maxn = 410;
const int N = 400;
const ll mod = 998244353;
inline ll qpow(ll b, ll p) {
ll res = 1;
while (p) {
if (p & 1) {
res = res * b % mod;
}
b = b * b % mod;
p >>= 1;
}
return res;
}
ll n, m, fac[maxn], ifac[maxn], f[maxn][maxn], g[maxn][maxn];
inline ll C(ll n, ll m) {
if (n < m || n < 0 || m < 0) {
return 0;
} else {
return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
}
inline void init() {
fac[0] = 1;
for (int i = 1; i <= N; ++i) {
fac[i] = fac[i - 1] * i % mod;
}
ifac[N] = qpow(fac[N], mod - 2);
for (int i = N - 1; ~i; --i) {
ifac[i] = ifac[i + 1] * (i + 1) % mod;
}
}
inline ll query(int xl, int xr, int yl, int yr) {
return (g[xl - 1][yl - 1] + g[xr][yr] - g[xl - 1][yr] - g[xr][yl - 1] + mod + mod) % mod;
}
void solve() {
scanf("%lld%lld", &n, &m);
for (int i = 1; i <= m; ++i) {
f[i][m] = 1;
}
ll ans = (n * (qpow(2, m) + mod - 1) % mod + 1) % mod;
for (int i = 2; i <= n; ++i) {
for (int j = 1; j <= m + 1; ++j) {
for (int k = 1; k <= m + 1; ++k) {
g[j][k] = (f[j][k] + g[j - 1][k] + g[j][k - 1] - g[j - 1][k - 1] + mod) % mod;
}
}
for (int j = 1; j <= m; ++j) {
for (int k = j; k <= m; ++k) {
f[j][k] = query(j, k + 1, k, m);
ans = (ans + f[j][k] * C(n, i) % mod * C(m, m - j + 1) % mod) % mod;
}
}
}
printf("%lld\n", ans);
}
int main() {
init();
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}