題目原文: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;
}