Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess(DP+组合数取模)(好题)

C. Gerald and Giant Chess
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 anh × 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 modulo109 + 7.

Sample test(s)
input
3 4 2
2 2
2 3
output
2
input
100 100 3
15 16
16 15
99 88
output
545732279


大致题意:

n,m 大小为1e5的矩形,求(1,1)点到(n,m)路径有多少中,中间有2000个不能经过点


思路:

想了好久才想出,还是太弱

首先不考虑不能经过的点,从(1,1)到任意一点(x,y)的路径的个数就是组合公式:C(x-1+y-1,x-1)

然后再考虑不能经过的点,正面艹了好久,发现搞不出来

直接从反面来,求出全集,再减去从不能经过的点到(x,y)的路径数。转移也很好


组合数取模用费马小引理之类的不懂的东西= =,用了Q神的模版


#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
typedef pair<int,int> pii;


const int N = 2000+100;
const int mod = 1e9+7;
ll dp[N];
int n,m,c;
pii poi[N];
int f[200000+5],inv[200000+5];
int fast_pow(int a,int k)
{
    int res=1;
    while(k>0)
    {
        if(k&1)res=1LL*res*a%mod;
        a=1LL*a*a%mod;
        k>>=1;
    }
    return res;
}
int C(int n,int k)
{
    if(n<k)return 0;
    return 1LL*f[n]*inv[k]%mod*inv[n-k]%mod;
}
void build()
{
    f[0]=1;
    for(int i=1;i<=200000;i++)
        f[i]=1LL*i*f[i-1]%mod;
    for(int i=0;i<=200000;i++)
        inv[i]=fast_pow(f[i],mod-2);
}
int main(){

        build();
        scanf("%d%d%d",&n,&m,&c);
        REP(i,c) scanf("%d%d",&poi[i].X,&poi[i].Y);
        sort(poi+1,poi+c+1);
        if(poi[c] == pii(n,m) ) {
                puts("0");
                return 0;
        }
        poi[c+1] = pii(n,m);
        REP(i,c+1){
                int x = poi[i].X, y = poi[i].Y;
                dp[i] = C(x-1+y-1,x-1);
                REP(j,i-1) {
                        int lx = poi[j].X, ly = poi[j].Y;
                        if( lx <= x && ly <= y)
                                dp[i] = (dp[i]-dp[j]*C(x-lx+y-ly,x-lx))%mod;
                }
        }
        printf("%d\n",(dp[c+1]%mod+mod)%mod);
}









你可能感兴趣的:(Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess(DP+组合数取模)(好题))