天天看点

codeforces 560E Gerald and Giant Chess (dp + 组合数)

题目原文:http://codeforces.com/contest/560/problem/E

E. 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 ≤ 105, 1 ≤ n ≤ 2000).

Next n lines contain the description of black cells. The i-th of these lines contains numbers ri, ci (1 ≤ ri ≤ h, 1 ≤ ci ≤ w) — 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 109 + 7.

题目大意:有一个n * m的棋盘,中间有不多于2000个点被选定,不能走,问从左上角到右下角有多少种走法?

解题思路:

首先考虑没有不能走的方格的情况,这就变成了高中组合数学的问题,C(h+w-2,h-1)

再考虑有一个不能走的方格的情况,需要用上面的值减去经过这个方格的情况(也就是C(x+y-2,x-1)*C(h-x+w-y,h-x))

所以我们考虑向多个不能走的方格推广。

我们定义 f[i] 为从左上角到这里经过前面那些点干扰的情况下有多少种走法,转移的办法就是每次减去在(x,y)这个矩形里面的点对后面点的干扰。

AC代码:

/*
    @Author: wchhlbt
    @Date:   2017/5/11
*/
#include <bits/stdc++.h>

#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 200022
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
using namespace std;

typedef long long ll ;
const double eps =1e-8;
const int mod = 1000000007;
typedef pair<int, int> P;
const double PI = acos(-1.0);
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

struct Black{
    ll x,y;
}b[2400];
ll f[maxn],inv[maxn],dp[2005];
bool cmp(Black r, Black s)
{
    if(r.x==s.x)    return r.y < s.y;
    return r.x < s.x;
}

ll quickpow(ll m,ll n,ll k)     // return m^n % k
{
    ll b = 1;
    m = m%k;
    while (n > 0)
    {
          if (n & 1)
             b = (b*m)%k;
          n = n >> 1 ;
          m = (m*m)%k;
    }
    return b;
}

ll C(ll n, ll m)
{
    return (((f[n] * inv[m])%mod) * inv[n-m]) % mod;
}

void init()
{
    f[0] = 1;
    for(int i = 1; i<=maxn-2; i++)
        f[i] = (i*f[i-1])%mod;
    inv[maxn-2] = quickpow(f[maxn-2],mod-2,mod);
    for(int i = maxn - 3; i>=0; i--)
        inv[i] = ( (i+1) * inv[i+1] ) % mod;
}

int main()
{
    //freopen("input.txt","r",stdin);
    //cout << inv[0] << endl;
    init();
    ll h,w,n;
    scanf("%I64d%I64d%I64d",&h,&w,&n);
    for(int i = 0; i<n; i++){
        scanf("%I64d%I64d",&b[i].x, &b[i].y);
    }
    sort(b,b+n,cmp);
    b[n].x = h; b[n].y = w;
    for(int i = 0; i<=n; i++){
        dp[i] = C( b[i].x + b[i].y - 2, b[i].x - 1);
        for(int j = 0; j<i; j++){
            if(b[j].y<=b[i].y){
                dp[i] = (dp[i] - dp[j] * C(b[i].x + b[i].y - b[j].x - b[j].y, b[i].x - b[j].x))%mod;
                dp[i] = (dp[i] + mod)%mod;
            }
        }
    }
    printf("%I64d\n",dp[n]);
    return 0;
}