Gerald and Giant Chess (CodeForces - 560E)(组合数)

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.

Example
Input
3 4 2
2 2
2 3
Output
2
Input
100 100 3
15 16
16 15
99 88
Output
545732279

#include  
#include  
#include  
#include  
#include   
#include
#include
#define mod 1000000007
using namespace std;

long long fac[200005],inv[200005];
long long sum[2005];
int h,w;
long long pow(long long x,int k)
{
    long long ans=1;
    while(k)
    {
        if(k&1)
            ans=ans* x%mod;
        x=x*x%mod;
        k>>=1;
    } 
    return ans;
} 
void init()
{
    fac[0]=inv[0]=1;
    for(int i=1;i<=h+w;i++)
        fac[i]=fac[i-1]*i%mod;
    int c=max(h,w);
     inv[c]=pow(fac[c],mod-2);
     for(int i=c-1;i>=1;i--)
        inv[i]=inv[i+1]*(i+1)%mod;   
}
struct node
{
    int x,y;
};
int cmp(node a,node b)
{
    return a.x2005];
int main()  
{  
    int n;
    scanf("%d%d%d",&h,&w,&n);
    init();
    for(int i=0;i"%d%d",&p[i].x,&p[i].y);
    p[n].x=h;p[n++].y=w;
    sort(p,p+n,cmp);

    int x1,y1,x2,y2;
    for(int i=0;i1;
        y1=p[i].y-1;
        sum[i]=fac[x1+y1]*inv[x1]%mod*inv[y1]%mod; 
        for(int j=0;jif(p[j].x<=p[i].x&&p[j].y<=p[i].y)
            {
                x2=x1-p[j].x+1;
                y2=y1-p[j].y+1;
                sum[i]=(sum[i]-fac[x2+y2]*inv[x2]%mod*inv[y2]%mod*sum[j] %mod)%mod;
                if(sum[i]<0)sum[i]=(sum[i]+mod)%mod;
            } 
        }
    } 
     printf("%lld\n",sum[n-1]);
    return 0;  
} 

你可能感兴趣的:(组合数,codeforces)