思路:
首先考虑n,m<=1000的情况,显然暴力dp转移即可.我们主要讨论n,m<=100000的情况.由于总格子数太多,但是障碍物很少,于是我们正难则反,考虑经过至少一个格子的情况,然后用总情况数减去这些情况就是结果.有了这个思想的启发,我们可以这样设计dp状态,f[i]表示不经过任何其他障碍到达第i个障碍的总方案数,看到恰好经过一个障碍物,我们套路地使用容斥:
,然后这道题就做完了,注意坐标的变换,使得计算更方便.
代码:
#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define ll long long
#define double long double
#define ull unsigned long long
#define PII pair<int, int>
#define PDI pair<double, int>
#define PDD pair<double, double>
#define debug(a) cout << #a << " = " << a << endl
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end()
#define mem(x, y) memset((x), (y), sizeof(x))
#define lbt(x) (x & (-x))
#define SZ(x) ((x).size())
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
using namespace std;
const int N = 2e6 + 10, M = 1010, MOD = 998244353;
int n, m, k, fact[N], infact[N], inv[N], f[N], ff[M][M], kk[M][M];
struct rec {
int x, y;
bool operator< (const rec& i) {
return x == i.x ? y < i.y : x < i.x;
}
} cor[N];
void init(int x) {
fact[0] = infact[0] = inv[0] = inv[1] = 1;
for (int i = 2; i <= x; ++i) inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
for (int i = 1; i <= x; ++i) {
fact[i] = fact[i - 1] * i % MOD;
infact[i] = infact[i - 1] * inv[i] % MOD;
}
}
int C(int n, int m) {
if (n < m) return 0;
if (m == 0 || n == m) return 1;
return fact[n] * infact[m] % MOD * infact[n - m] % MOD;
}
signed main() {
init(2e5);
qio >> n >> m >> k;
if (k == 0) return qio << C(n + m, n) << "\n", 0;
cor[0] = {0, 1}, cor[k + 1] = {n, m + 1};
for (int i = 1; i <= k; ++i) {
int x, y;
qio >> x >> y;
if (x <= 1000 && y <= 1000)
kk[x][y] = 1;
x = n + 1 - x;
cor[i] = {x, y};
}
if (k <= 2000) {
sort(cor + 1, cor + k + 1);
f[0] = 1;
for (int i = 1; i <= k + 1; ++i) {
f[i] = C(cor[i].x + cor[i].y - 1, cor[i].x);
for (int j = 1; j <= i - 1; ++j)
if (cor[j].x <= cor[i].x && cor[j].y <= cor[i].y)
f[i] = (f[i] - f[j] * C(cor[i].x - cor[j].x + cor[i].y - cor[j].y, cor[i].x - cor[j].x)) % MOD;
}
qio << (f[k + 1] + MOD) % MOD << "\n";
}else {
ff[n + 1][1] = 1;
for (int i = n + 1; i >= 1; --i)
for (int j = 1; j <= m + 1; ++j) {
if (!kk[i - 1][j]) ff[i - 1][j] = (ff[i - 1][j] + ff[i][j]) % MOD;
if (!kk[i][j + 1]) ff[i][j + 1] = (ff[i][j + 1] + ff[i][j]) % MOD;
}
qio << ff[1][m + 1] % MOD << "\n";
}
}