当前位置: 首页 > 工具软件 > Some Chess > 使用案例 >

codeforces559C Gerald and Giant Chess(计数dp)

颜杰
2023-12-01

Gerald and Giant Chess

Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we’ll just say that the game takes place on an h × w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win?

The pawn, which Gerald has got left can go in two ways: one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is just watching this process.

Input

The first line of the input contains three integers: h, w, n — the sides of the board and the number of black cells ( 1   ≤   h ,   w   ≤   1 0 5 ,   1   ≤   n   ≤   2000 ) (1 ≤ h, w ≤ 10^5, 1 ≤ n ≤ 2000) (1h,w105,1n2000).

Next n n n lines contain the description of black cells. The i-th of these lines contains numbers r i ,   c i ( 1   ≤   r i   ≤   h ,   1   ≤   c i   ≤   w ) r_i, c_i (1 ≤ r_i ≤ h, 1 ≤ c_i ≤ w) ri,ci(1rih,1ciw) — the number of the row and column of the i-th cell.

It is guaranteed that the upper left and lower right cell are white and all cells in the description are distinct.

Output

Print a single line — the remainder of the number of ways to move Gerald’s pawn from the upper left to the lower right corner modulo 1 0 9   +   7 10^9 + 7 109+7.

思路

将黑色方块按照x坐标,y坐标递增排序。f[i]代表从左上角出发,第一次在 x i , y i x_i,y_i xi,yi这个位置踩到黑色方块,可能的路径数量。

于是有状态转移方程:
f [ i ] = C h i + w i − 2 h i − 1 − ∑ j = 1 j = i − 1 f [ j ] × C h i + w i − h j − w j h i − h j f[i]=C^{h_i-1}_{h_i+w_i-2}-\sum_{j=1}^{j=i-1}f[j]\times C^{h_i-h_j}_{h_i+w_i-h_j-w_j} f[i]=Chi+wi2hi1j=1j=i1f[j]×Chi+wihjwjhihj
设第 0 0 0个黑色方块为 { 1 , 1 } \{1,1\} {1,1},第 n + 1 n+1 n+1个黑色方块为 { h , w } \{h,w\} {h,w},那么所求答案为 f [ n + 1 ] f[n+1] f[n+1]

可以先预处理出所有的阶乘,以及阶乘的逆元,再进行计算。

#include<cstdio>
//#include<utility>
#include<algorithm>
#define int long long
#define x first
#define y second
using namespace std;
const int mod=1e9+7;
int h,w,n,jc[200010],jcinv[200010],f[2010];
pair<int,int> a[2010];

int qpow(int a,int b){
    int ret=1;
    while(b){
        if(b&1) ret=ret*a%mod;
        b>>=1,a=a*a%mod;
    }
    return ret;
}

int C(int up,int down){
    return jc[down]*jcinv[up]%mod*jcinv[down-up]%mod;
}

signed main(){
    jc[0]=1;    jcinv[0]=1; f[0]=1;
    for(int i=1;i<=200000;i++){
        jc[i]=i*jc[i-1]%mod;
        jcinv[i]=qpow(jc[i],mod-2);
    }
    scanf("%lld%lld%lld",&h,&w,&n);
    for(int i=1;i<=n;i++)   scanf("%lld%lld",&a[i].x,&a[i].y);
    sort(a+1,a+n+1);
    a[n+1].x=h,a[n+1].y=w;
    for(int i=1;i<=n+1;i++){
        f[i]=C(a[i].x-1,a[i].x+a[i].y-2);
        for(int j=1;j<i;j++){
            if(a[j].y>a[i].y)   continue;
            f[i]=(f[i]-f[j]*C(a[i].x-a[j].x,a[i].x+a[i].y-a[j].x-a[j].y))%mod;
        }
    }
    printf("%lld\n",(f[n+1]+mod)%mod);
}
 类似资料: