Codeforces-559C 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.
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。 InputThe 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.
OutputPrint 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.
Examples Input Copy3 4 2Output Copy
2 2
2 3
2Input Copy
100 100 3Output Copy
15 16
16 15
99 88
545732279
题目大意:从点(1,1)走到(h,w),只能向右或向下,给出n个黑点点,不经过这些点走到(h,w)的路径有多少条(mod 1e9+7)
1 ≤ h, w ≤ 105, 1 ≤ n ≤ 2000
思路:对于每个点i(x1,y1),若不考虑黑点,从(1,1)到i有C(x1+y1-2)(x1-1)条路径
考虑黑点,记s[i] = C(x1+y1-2)(x1-1),若存在点j(x2,y2)在点i的左上,则s[i]应该减去C(x1-x2+y1-y2)(x1-x2)*s[j],减去所有点i左上的黑点的影响,即得到s[i]
从右下第一个黑点计算到点(h,w)
C(x+y)(x) = c[x+y]*inv[x]*inv[y]%mod
c[k]:k的阶乘
inv[k]:k的逆元
逆元由公式inv[i] = inv[i+1] * (i+1) % mod 求得

#include<cstdio> #include<iostream> #include<algorithm> using namespace std; const int mod=1e9+7; const int N = 2e5+10; struct Node{ int r; int c; }node[2005]; long long s[2005],c[N],inv[N]; bool cmp(Node a,Node b){ if(a.c==b.c) return a.r<b.r; return a.c<b.c; } void Getfac(int d){ c[0]=1; for(int i = 1;i<=d;i++){ c[i]=c[i-1]*i%mod; } } long long quick_inv(int n){ long long ans = 1,pos = n; int y = mod - 2; while(y){ if(y%2) ans = ans*pos%mod; pos = pos*pos%mod; y = y/2; } return ans%mod; } void Getinv(int d){ inv[d] = quick_inv(c[d]); for(int i = d-1;i>=0;i--){ inv[i] = inv[i+1]*(i+1)%mod; } } int main() { int h,w,n; long long k,t; scanf("%d%d%d",&h,&w,&n); for(int i = 1;i <= n;i++){ scanf("%d%d",&node[i].r,&node[i].c); } node[0].r = 1; node[0].c = 1; node[n+1].r = h; node[n+1].c = w; s[0] = 1; sort(node+1,node+n+1,cmp); Getfac(h+w); Getinv(h+w); for(int i = 1;i <= n+1;i++){ k = node[i].c-1; t = node[i].r-1; s[i] = c[k+t]*inv[k]%mod*inv[t]%mod; for(int j = 1;j < i;j++){ k = node[i].c - node[j].c; t = node[i].r - node[j].r; if(k>=0&&t>=0) s[i] -= c[k+t]*inv[k]%mod*inv[t]%mod *s[j]%mod; } s[i] = (s[i]+mod)%mod; } s[n+1] = (s[n+1]+mod)%mod; printf("%lld\n",s[n+1]); return 0; }View Code
