---
id: 781
lang: en
translation_of: index.md
source_sha256: 3e2cf4ea575e78bdcac772ccddac368dcf9e5a537d784a8f86a3d7ea96718b39
title: "[Codeforces 559C] Gerald and Giant Chess — Solution"
description: "Total paths from the top-left to the bottom-right corner, minus those through at least one black cell, via an O(n^2) inclusion–exclusion DP on sorted cells."
tags: ["Dynamic Programming","OI","DP","Counting","Counting DP"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "The Chinese words ，且 (“, and”) inside the display formula are not in \\text{…}, so they are kept verbatim as part of the math."
  - "黑点 (literally “black point”) rendered as “black cell”; 点 on its own as “point”."
---

## Main Idea

This problem is actually pretty nice, so let's analyze it.

First, we know that on a sheet of grid paper, the number of paths from the top-left corner to the bottom-right corner is $C_{H+W-2}^{H-1}$, which can be understood as the total number of ways to choose the horizontal steps. After that, we do inclusion–exclusion. In this problem, we need to exclude the number of paths that pass through a black cell at least once. Since the number of black cells in this problem is less than $5000$, we just run an $O(n^2)$ DP directly. Let $F[i]$ be the number of paths that pass through this point; then we set the bottom-right point as point number $n+1$, and the final answer is stored at $F[n+1]$. We can quickly get a recurrence:

$$
F[i] = C_{x_i + y_i - 2}^{x_i - 1} - \sum_{j = 1}^{i-1} (F[j] * C_{x_i-x_j+y_i-y_j}^{x_i-x_j})，且x_i \geq x_j, y_i \geq y_j.
$$

Explanation: for the paths to point $i$, if we first ignore the earlier black cells, there are $C_{x_i + y_i - 2}^{x_i - 1}$ paths; and to exclude the paths that pass through a black cell at least once, we can directly compute for each earlier point how many paths there are from the earlier point $j$ to the current point $i$.

## Code

```cpp
// CF559C.cpp
#include <bits/stdc++.h>
#define ll long long
#define pr pair<ll, ll>
using namespace std;
const int MOD = 1e9 + 7, MAX_N = 2020, LEVEL_RG = 200000;
ll level[200010], inv[200010], n, h, w, F[MAX_N];
pr pts[MAX_N];
ll quick_power(ll bas, ll tim, ll mod)
{
    ll ans = 1;
    while (tim)
    {
        if (tim & 1)
            ans = ans * bas % mod;
        bas = bas * bas % mod;
        tim >>= 1;
    }
    return ans;
}
ll C(ll n, ll m) { return level[n] * inv[n - m] % MOD * inv[m] % MOD; }
int main()
{
    scanf("%d%d%d", &h, &w, &n);
    for (int i = 1; i <= n; i++)
        scanf("%d%d", &pts[i].first, &pts[i].second);
    sort(pts + 1, pts + 1 + n);
    pts[n + 1].first = h, pts[n + 1].second = w;
    level[0] = inv[0] = 1;
    for (int i = 1; i <= LEVEL_RG; i++)
        level[i] = level[i - 1] * i % MOD;
    ll inv_bas = quick_power(level[LEVEL_RG], MOD - 2, MOD);
    for (int i = LEVEL_RG; i >= 1; i--)
        inv[i] = inv_bas, inv_bas = inv_bas * i % MOD;
    for (int i = 1; i <= n + 1; i++)
    {
        F[i] = C(pts[i].first + pts[i].second - 2, pts[i].first - 1) % MOD;
        for (int j = 1; j < i; j++)
            if (pts[j].first <= pts[i].first && pts[j].second <= pts[i].second)
                F[i] = (F[i] - F[j] * C(pts[i].first + pts[i].second - pts[j].first - pts[j].second, pts[i].first - pts[j].first)) % MOD;
    }
    printf("%lld", (F[n + 1] + MOD) % MOD);
    return 0;
}
```
