# 代码源 467 路径计数 2 题解

## Solution

$\dbinom{2n-2}{n-1}-\sum_{i=1}^m f_i\cdot\dbinom{2n-x_i-y_i}{n-x_i}$

$$f$$ 可以通过枚举在其之前的障碍点转移：

$f_i=\dbinom{x_i+y_i-2}{x_i-1}-\sum_{j=1}^{i-1} f_j\cdot\dbinom{x_i-x_j+y_i-y_j}{x_i-x_j}$

## Code

constexpr int mod = 1e9 + 7;
constexpr int N = 2000005;
constexpr int M = 3005;

std::pair<int, int> point[M];

inline int fp(int x, int y = mod - 2) {
int res = 1;
for (; y; y >>= 1, x = 1ll * x * x % mod) {
if (y & 1) {
res = 1ll * res * x % mod;
}
}
return res;
}

inline void init(const int &n) {
fac[0] = 1;
for (int i = 1; i <= n; ++i) {
fac[i] = 1ll * fac[i - 1] * i % mod;
}
ifac[n] = fp(fac[n]);
for (int i = n; i >= 1; --i) {
ifac[i - 1] = 1ll * ifac[i] * i % mod;
}
}

inline int binom(const int& n, const int& m) {
return n < m ? 0 : 1ll * fac[n] * ifac[n - m] % mod  * ifac[m] % mod;
}

int n, m;
in >> n >> m;
init(n << 1);
for (int i = 1; i <= m; ++i) {
int x, y;
in >> x >> y;
point[i] = std::make_pair(x, y);
}
std::sort(point + 1, point + 1 + m);
point[m + 1] = std::make_pair(n, n);
for (int i = 1; i <= m + 1; ++i) {
auto [x_i, y_i] = point[i];
f[i] = binom(x_i + y_i - 2, x_i - 1);
for (int j = 1; j < i; ++j) {
auto [x_j, y_j] = point[j];
if (x_j > x_i || y_j > y_i) {
continue;
}
f[i] = (f[i] - 1ll * f[j] * binom(x_i + y_i - x_j - y_j, x_i - x_j) % mod + mod) % mod;
}
}
out << f[m + 1] << '\n';
}

posted @ 2022-03-06 12:42  feicheng  阅读(126)  评论(0编辑  收藏  举报